casting

Why does C# throw casting errors when attempting math operations on integer types other than int?

Consider this static test class: public static class Test { public static ushort sum(ushort value1, ushort value2) { return value1 + value2 } } This causes the following compile error, with value1 + value2 underlined in red: Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (ar...

Why is this named function not allowed to be passed as a Func<> argument in C#?

I am having trouble understanding what the rules are for passing delegates in C#3.0. The final .Max() is giving the compile error as shown below, but I can't understand what the significant difference is between that case and any of the others. int NamedIdentity(int val) { return val; } Func<int, int> Identity = x => x; void Main() { ...

Typedef reinterpret_cast

Hi, I am having some trouble figuring out the syntax for typedef reinterpret_cast. Can anyone help? EDIT What I am trying to do. I am always hesitant as people seem to get caught up in every thing else except what the problem actually is as you can see with my last post which lead to a whole bunch of nothing. I am trying to come up w...

How to know when to cast to an Xelement or XAttribute?

I have a situation where an end user can enter an XPath to access a value in some XML. I’m using a line of code similar to the one below: IEnumerable e = (IEnumerable)importDocument.XPathEvaluate(theXPath); As the Xpath could return an Attribute or an Element, what I need to know is how can I interpret ‘e’ in the above example to deci...

How can I do a safe downcast and prevent a ClassCastException

Hi, I have the following scenario: public class A { } public class B extends A { } public class C extends B { public void Foo(); } I have a method that can return me class A, B or C and I want to cast safely to C but only if the class I get is of type C. I need to call Foo() but I don't want the ClassCastException. ...

Casting with multiple inheritance

If you have a void* pointer to Derived class that inherits from both BaseA and BaseB, how does the compiler cast the void* pointer to BaseA* (or BaseB*) without knowing that the void* pointer is of type Derived? ...

Specialize a generic variable

I have a generic method where I want to do something special for Strings. I've found DirectCast(DirectCast(value, Object), String) to get the String value (when I've already confirmed GetType(T) Is GetType(String)) and DirectCast(DirectCast(newvalue, Object), T) as mentioned in a number of answers to similar questions works. But is the...

Encapsulating generic database methods in a Java superclass and then calling from Child classes doesn't work without a cast. HELP

I'm having a heap of trouble with Java coming from a PHP background. I've got a parent class Entity containing generic database methods, such as a static method getById(int id). My aim is to have children of this class, such as Person, so that I can call: Person p = Person.getById(1); At the moment this doesn't work, as getById(1) re...

How can I use a nullable int or a NSInteger pointer?

I need a value to be calculated in it's getter method if it isn't set already. I prefer to use a nullable int construction, so I don't need another bool to check if it's already been calculated. My first hunch was to go for a NSInteger *. I can check if it's NULL and otherwise set a value to it. But I don't know if it's possible, since ...

php convert to byte ,

in java i can cast number to byte for example System.err.println((byte)13020); the result will be -36 how can i do the same in php ? ...

Casting and Linq Cast<T>()

When trying to answer this question, I discovered the following: string s = "test"; var result1 = s.Select(c => (ushort)c); // works fine var result2 = s.Cast<ushort>(); // throws an invalid cast exception Why does Cast<T>() fail here? Whats the difference? ...

Using properties of objects in a listbox

I've got this nifty little piece of code here that self-propogates ad infinitum every time the relevant method is called: if (this.ListBox_MyListBox.Items[e.Index] is MyObject) { MyObject epicObject= new MyObject(); epicObject= (MyObject)this.ListBox_MyListBox.Items[e.I...

Casting Exceptions in C#

Why do I get an InvalidCastException when trying to do this? throw (ArgumentNullException)(new Exception("errormessage", null)); This is a simplified version of the following function. public static void Require<T>(bool assertion, string message, Exception innerException) where T: Exception { if (!assertion) { ...

PHP unexpected result of float to int type cast

I'trying to convert a float to an int value in php: var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930! var_dump((int)(39.2 * 100.0)); //Returns 3920 I can use ceil to make it work but can somebody explain this to me? var_dump((int)ceil(39.3 * 100.0)); //Returns 3930 ...

Why does the compiler think this is an Object instead of a DataRow?

I'm using a LINQ query to translate data inside a DataTable object to be a simple IEnumerable of a custom POCO object. My LINQ query is: Dim dtMessages As DataTable '...dtMessages is instantiated ByRef in a helper data access routine... ' Dim qry = From dr As DataRow In dtMessages.Rows Select New OutboxMsg...

GLSL - Problem of casting and weird bug

Hi, i'm here to get help about a strange behavior of my GLSL code when i cast a float to an int and i never seen such a bug since i started GLSL Actually i'm trying to achieve mesh skinning on CPU with GLSL I use an ATI Radeon HD 4850 (Gainward) and i work with OpenGL 2.1 on Windows XP so on CPU side I gather bones indices and weights...

Casting entire array of objects to string

I have an array of type object which are strings. I would like to convert them to strings what would be the quickest way of doing so. Eg. I have this "object[]" and want to convert it so it is this "string[]" UPDATE - i think the problem is that some of the objects on the object[] are actually other objects like integers. I would ...

string = string + int: What's behind the scene? (C#)

In C# you can implicite concat string and let's say, an integer: string sth = "something" + 0; My questions are: Why, by assuming fact you can implicite concat string and int, C# disallow initializing string as: string sth = 0; // Error: Cannot convert source type 'int' to target type 'string' How C# casts 0 as string. Is it 0.ToSt...

const_cast vs static_cast

To add const to a non-const object, which is the prefered method? const_cast<T> or static_cast<T>. In a recent question, someone mentioned that they prefer to use static_cast, but I would have thought that const_cast would make the intention of the code more clear. So what is the argument for using static_cast to make a variable const? ...

Why casting to object when comparing to null ?

While browsing the MSDN documentations on Equals overrides, one point grabbed my attention. On the examples of this specific page, some null checks are made, and the objects are casted to the System.Object type when doing the comparison : public override bool Equals(System.Object obj) { // If parameter is null return false. if ...