casting

C++: how to cast 2 bytes in an array to an unsigned short

I have been working on a legacy c++ application and am definitely outside of my comfort-zone (a good thing), was wondering if anyone out there would be so kind as to give me a few pointers (pun intended) I need to cast 2 bytes in an unsigned char array to an unsigned short. The bytes are consecutive. For an example of what I am trying...

Exception when casting from concrete class to interface in LINQ query.

Hi, I have a class SomeClass, which can populate itself from a datarow in it's constructor. This class implements IInterface. However, when I execute the code below: Dim fpQuery As IEnumerable(Of IInterface) = _ From dr As DataRow In DataLayer.SomeMethodToGetADataTable.AsEnumerable _ Select New SomeClass(dr) I get the error ...

VB6 Cast Expression

What is the cast expression equivalent of VB.NET's CType in Visual Basic 6? ...

How do I cast id to a float?

I'm having trouble with this code: NSRect itemFrame; id item; // code to assign item goes here. itemFrame.origin.y -= [item respondsToSelector:@selector(selectedHeight)] ? [item selectedHeight] : [self defaultSelectedHeight]; This is the problematic bit: [item selectedHeight] The compiler is assuming that the return type is id. I...

Error CASTing value to FLOAT in SQL Server 2005

I'm trying to get a stored procedure to work for a co-worker who is out sick (and thus can't be asked for guidance). I have a SQL Server 2005 database that has this exact procedure, and I'm trying to make the scripts to convert a test database to match this dev database. My script has several lines like: CAST(RELATIVE_ERROR_RATIO AS F...

Why do I need to cast self to id?

I have an init method that takes an (id) argument: -(id) initWithObject:(id) obj; I'm trying to call it like this: [[MyClass alloc] initWithObject:self]; But XCode is complaining about the argument being a "distinct Objective-C type" (which usually indicates a type mismatch or level of indirection error). If I explicitly...

static_cast confusion caused by inconsistencies

Why whenever I compile and run the following code in Visual Studio 2008: double value1 = 10.5; double value2 = 15.5; int whole_number = value1 + value2; Console::WriteLine(whole_number); I get an incorrect value of 26 while the answer is 25. However when I use static casts on the doubles, I get the right answer which is 25. How can ...

How should I cast for Java generic with multiple bounds?

Is it possible to cast an object in Java to a combined generic type? I have a method like: public static <T extends Foo & Bar> void doSomething(T object) { //do stuff } Calling this method is no problem if I have a class that implements both interfaces (Foo & Bar). The problem is when I need to call this method the object I need...

Type Casting

Hey guys, I want to pass in the Type of an class to a function, and the class object to a generic function. I need to be able to cast to that Type (of class) so i can access the class's methods. Something Like: void GenericFunction(Object obj, Type type) { (type)obj.someContainer.Add(1); } would implementing an interface for t...

Check for any int type in C#?

I have a function that, among other things, takes in an object and a Type, and converts the object into that Type. However, the input object is often a double, and the type some variation of int (uint, long, etc.). I want this to work if a round number is passed in as a double (like 4.0), but to throw an exception if a decimal is passe...

Is there a type of object, to which I can cast both Buttons AND Menutem objects in order to access their Tag properties?

I want to cast both MenuItem objects and Button control objects to an object type of whose "Tag" property I can reference. Is there such an object type? E.g. void itemClick(object sender, EventArgs e) { Control c = (Control)sender; MethodInvoker method = new MethodInvoker(c.Tag.ToString(), "Execute"); method.Invoke(); } ...

Why do I have to cast enums to int in C#?

This is my code: internal enum WindowsMessagesFlags { WM_EXITSIZEMOVE = 0x00000232, WM_DISPLAYCHANGE = 0x0000007e, WM_MOVING = 0x00000216, } protected override void WndProc(ref Message m) { switch(m.Msg) { case (int)WindowsMessagesFlags.WM_DISPLAYCHANGE: FixWindowSnapping(); break; ...

When should static_cast, dynamic_cast and reinterpret_cast be used?

I am reasonably proficient in C++, but I do not have a lot of experience using the cast operators to convert pointers of one type to another. I am familiar with the risks and benefits of pointer casting, as well as the evils of using C-style casts. What I am looking for is a primer on the proper ways to use the various cast operators in ...

In C#, how can I downcast a previously upcasted object without knowing it's type?

I have an interface method public void Execute(ICommand command); which needs to pass known subtypes of ICommand to an apropriate Handle(SpecificCommand command) method implementation and do some generic handling of unknown types. I am looking for a universal (i.e. not requiring a giant switch) method of doing so, something similar ...

what's the runtime equivalent of c# 'bracketed' type cast

suppose I have an enum [Flags] public enum E { zero = 0, one = 1 } then I can write E e; object o = 1; e = (E) o; and it will work. BUT if I try to do that at runtime, like (o as IConvertible).ToType(typeof(E), null) it will throw InvalidCastException. So, is there something that I can invoke at runtime, and it will c...

Opinions on type-punning in C++?

I'm curious about conventions for type-punning pointers/arrays in C++. Here's the use case I have at the moment: Compute a simple 32-bit checksum over a binary blob of data by treating it as an array of 32-bit integers (we know its total length is a multiple of 4), and then summing up all values and ignoring overflow. I would expect...

List Generics and Casting

Hello everyone. I have two classes: Media and Container. I have two lists List<Media> and List<Container> I'm passing these lists to another function (one at a time); it can be one or another; what's the proper way to check for the "template" type of the list so i can call an asssociated method depending on the list type? or shoul...

Is const_cast safe?

I can't find much information on const_cast. The only info I could find (on Stack Overflow) is: The const_cast<>() is used to add/remove const(ness) (or volatile-ness) of a variable. This makes me nervous. Could using a const_cast cause unexpected behavior? If so, what? Alternatively, when is it okay to use const_cast? ...

Casting between ArrayLists in Java

Hi Sorry, I thought this was an inheritance question: it was an ArrayList question all along! Ok, my problem is more specific than I thought. So I have two families of classes. Cards, and Zones. Zones are boxes for holding card. The first two subClasses of Zone, ZoneList and ZoneMap are meant to be two different ways of storing Cards...

Why is the default length for VARCHAR 30 when using CAST?

In SQL server 2005 this query select len(cast('the quick brown fox jumped over the lazy dog' as varchar)) returns 30 as length while the supplied string has more characters. This seems to be the default. Why 30, and not 32 or any other power of 2? ...