casting

Why is "operator bool()" invoked when I cast to "long"?

I have the following class: class MyClass { public: MyClass( char* what ) : controlled( what ) {} ~MyClass() { delete[] controlled; } operator char*() const { return controlled; } operator void*() const { return controlled; } operator bool() const { return controlled != 0; } private: char* controlled; }; This is com...

AS3: global var of type SimpleButton changes to DisplayObject for unknown reason, won't let me access .upState.textColor!

This is a problem that's best explained in code. I don't see how active_button.upState, which I know is a TextField (see trace statements), mysteriously turns into a DisplayObject when I try to access the textColor property. I've included the error messages below for reference. Also, why is it that when I have an object that I know i...

C# explicit casting of derived types

If I create class A, and class B inherits from class A, why does C# require me to explicitly cast between them? For example: public class Mammal { } public class Dog : Mammal { } ... Mammal foo = new Dog(); // Invalid, wants an explicit cast Mammal bar = (Mammal)new Dog(); // This one works I'm just curious what the reasoning is ...

AS3: Why does datatype automatically change from TextField to DisplayObject on its own?

What's happening in this simple piece of AS3 code? Why does my object change from TextField to the more generic DisplayObject? public class Menu extends MovieClip { private var active_button:SimpleButton; public function Menu() { active_button = SimpleButton( menu_list.getChildAt( 0 )); // ignore menu_lis...

Is there any runtime cost for Casting in Java?

Would there be any performance differences between these two chunks? public void doSomething(Supertype input) { Subtype foo = (Subtype)input; foo.methodA(); foo.methodB(); } vs. public void doSomething(Supertype input) { ((Subtype)input).methodA(); ((Subtype)input).methodB(); } Any other considerations or recomm...

Passing an explicit cast as a ref parameter (C#)

I have a class that is mostly a wrapper for a big array and some associated housekeeping. I have a function that takes a ref parameter. When I pass an instance of the class into the function, I want the array to get sent. I considered explicit casts. Let's say I have some function that has a byte[] ref parameter. public void Som...

Use struct as base for derived class in C++

Is it possible to have a class inheriting from a struct? More specifically, how can I wrap a C struct with a class, so that I can pass class pointers to methods that requires struct ptr and cast back when I receive the pointer in e.g. callbacks? (Or even more specifically, the address of the class should be same same address as the str...

Error comparing dates in SQL query

Within a ASP.NET/C# class I am trying to run a query where I compare dates: select * from table1 CreatedDate >='DATEADD(d,-500,GETDATE())'; Basically I am trying to select rows from the the last 500 days. The problem is I am getting the following error: Syntax error converting datetime from character string. An example of the Creat...

Member Pointer to Base Class

Hi, all. I can't undestand why the bellow code need a cast to work. Someone can explain it? class Base { }; class Derived : public Base { }; class Class { public: Derived member; }; ... Derived obj; Base *ptrObj = &obj; // ok, no cast needed Derived Class::* ptr = &Class::member; // ok Base Class::* ptr = &Class::member; // ...

accessing a System.Data.DataRow object in a multithreaded program

Hi, i am trying to extract a single row of data from a DataSet. Unfortunately since this program is multithreaded any changes made in a different thread to the DataSet are carried over to my separate DataRow object. I'm declaring the DataRow as below: Dim DR As System.Data.DataRow = DS.Tables(0).Rows(0) I SyncLock DS during the declar...

What is the leanest way to convert a Dictionary<string, string> to a Dictionary<string, object>?

I'm using an API that returns a key-value collection as a Dictionary<string, string>. I need to convert that to a Dictionary<string, object>. I have a sense that there should be a way to do this conversion/mapping without "manually" looping through each key-value pair, but Googling or the C# object reference didn't immediately yield a so...

Cleaner way to cast a C# enum

I want to use an object (returned by framework, not in my control), myField which has a property DisplayFormat of type enum SPNumberFormatTypes. I want to assign the integer value of DisplayFormat as a string to an XmlAttribute. Here is what I currently do: myAttribute.Value = ((Int32)((SPNumberFormatTypes)field.DisplayFormat)).ToStrin...

com.sun.org.apache.xerces.internal.dom.DocumentImpl cannot be cast to org.codehaus.plexus.util.xml.Xpp3Dom

Afetr using the Model library (see this) for my Maven pom.xml during this weeks I jumped on this error while attempting to write a ConfiguratonContainer into the pom.xml. The javadoc for Model says: public void setConfiguration(Object configuration) Set the configuration as DOM object. Parameters: configuration - So I mad...

Worst side effects from chars signedness. (Explanation of signedness effects on chars and casts)

I frequently work with libraries that use char when working with bytes in C++. The alternative is to define a "Byte" as unsigned char but that not the standard they decided to use. I frequently pass bytes from C# into the C++ dlls and cast them to char to work with the library. When casting ints to chars or chars to other simple types w...

Which kind of cast is from Type* to void*?

In C++ for any data type I can do the following: Type* typedPointer = obtain(); void* voidPointer = typedPointer; which cast is performed when I assign Type* to void*? Is this the same as Type* typedPointer = obtain(); void* voidPointer = reinterpret_cast<void*>( typedPointer ); or is it some other cast? ...

Rounding with static_cast<int> ?

Hi, I feel really silly asking this because I know how to do it 101 ways, but not the way it is defined in the book. (note, I know C++) So far, we have only gone over the very basics of C++. So basically, we know variables, assignment, and basic casting. In the book I am having trouble with this portion of the problem: prompt the us...

Why doesn't this reinterpret_cast compile?

I understand that reinterpret_cast is dangerous, I'm just doing this to test it. I have the following code: int x = 0; double y = reinterpret_cast<double>(x); When I try to compile the program, it gives me an error saying invalid cast from type 'float' to type 'double What's going on? I thought reinterpret_cast was the rogue cast t...

How to cast Object to boolean ?

How can I cast a Java object into a boolean primitive I tried like below but it doesn't work boolean di = new Boolean(someObject).booleanValue(); The constructor Boolean(Object) is undefined Please advise. ...

how to cast nullable tinyint in linq to sql ?

I want to cast Nullable Tinyint column to int value in Linq to SQL ? ...

JAVA RMI dispatchUncaughtException - Interface casting problem

For school we made a Java application with RMI, there are 3 applications: Server, Client and Rekenaar. The line where it goes wrong is the Line: "test = (Range[])m.getAllRange();", it gives an dispatchUncaughtException. package rekenaar; import server.Opdracht; import java.rmi.Naming; import java.util.logging.Level; import java.util.l...