cast

Java: Why can't I cast this to an Object?

This is bizarre... I thought every object in java had Object as an ancestor. I have a ClassA that extends my ClassB and implements Runnable. After creating ClassA I cannot cast it to an Object. Assume getClassA returns a ClassA instance. I am doing Object obj = getClassA(); I also tried Object obj = (Object) getClassA(); I get...

Generate Cast type dynamically in C#

Hello.. Please I have a class in c# whose main function is to return the types and objects as dictionary Over a service . Is it possible to cast the Object sents over the WCF service in the front end. I.e using reflection to get the type of an object from the types.ToString() and using the type to cast the objects. NB the Class that ...

Assign a type to an object at runtime in vb.net or c#

I have an object, o, and a type, T. I'd like to use reflection to change object o to type T at runtime without instantiating it. The equivalent at compile time would be: Dim p as Point = Nothing I know how to use Activator.CreateInstance to create an instance at run time that is equivalent to: Dim p as New Point() But i don't want...

Can I cast std::vector<Animal*> to std::vector<Dog*> without looking at each element?

I have a base class with several classes extending it. I have some generic library utilities that creates a vector containing pointers to the base class so that any of the subclasses will work. How can I cast all of the elements of the vector to a specific child class? // A method is called that assumes that a vector containing // Dogs ...

invalid converstion from void* to char**

it has been a while since I messed with C code. I am getting the following error when compiling C code under ubuntu using gcc. command i am using to compile code is(if these errors are because of comiler I am using , please let me know how to make that go away): gcc -o runnable mycode.C error: invalid conversion from ‘void*’ to ‘...

Base-to-derived class typecast

I have a base class: class RedBlackTreeNode { // Interface is the same as the implementation public: RedBlackTreeNode* left; RedBlackTreeNode* right; RedBlackTreeNode* parent; Color color; TreeNodeData* data; RedBlackTreeNode(): left(0), right(0), parent(0), color(Black), ...

Casting C# Soap Headers from one assembly to another

Briefly, I have been given a custom soap header class CustomHeader that contains a string representing the user's security token. public string UserInfo { get { return this.userInfoField; } set { this.userInfoField = value; } } I have two webservices that both use the sam...

How to pointer-cast Foo** to const Foo** in C++

I have class Fred { public: void inspect() const {}; void modify(){}; }; int main() { const Fred x = Fred(); Fred* p1; const Fred** q1 = reinterpret_cast<const Fred**>(&p1); *q1 = &x; p1->inspect(); p1->modify(); } How would it be possible to do the const Fred** q1 = &p1 via pointer-casting? (I have just been reading...

How to cast Generic Lists dynamically in C#?

I'm trying to cast List<object> to List<string> dynamically. I've tried several ways, but I can't find a solution. This is a small sample that shows the problem: List<object> listObject = new List<object>(); listObject.Add("ITEM 1"); listObject.Add("ITEM 2"); listObject.Add("ITEM 3"); List<string> listString = ¿¿listObject??; Thanks ...

How to type cast a literal in C

Hi, I have a small sample function: #define VALUE 0 int test(unsigned char x) { if (x>=VALUE) return 0; else return 1; } My compiler warns me that the comparison (x>=VALUE) is true in all cases, which is right, because x is an unsigned character and VALUE is defined with the value 0. So I changed my code to: if ( ((sign...

Vector Iterators Casting

Hey, In C++, I have a vector of type: vector<BaseClass*> myVector; In which, I insert (push_back) pointers of derived classes into it. Now, I want to pop back its elements so I do this: vector<ADlgcDev*>::iterator iter; for (iter = myVector.rbegin(); iter != myVector.rend(); iter++) { // but before I pop it, I need to shutdown it ...

Hibernate Groovy entities

Hi All! I'm have a groovy hibernate entity, named 'Depart' for example. Now when i'm try to get their property (for example Long id = somedepart.getIdDepart() ) i'm got an exception Cannot cast Depart to Depart_$$_javassist_5 Who make Depart_$$_javassist_5 - groovy or hibernate? Is there some workaround about this? ...

Problem while porting VB.NET Code to C#

Hi, Currently I am trying to port some VB.NET code to C#. The struct looks like this in VB.NET: Public Structure sPos Dim x, y, z As Single Function getSectorY() As Single Return Math.Floor(y / 192 + 92) End Function Function getSectorX() As Single Return Math.Floor(x / 192 + 135) End Function F...

Cast bool to T where T is int

How can I make this function reliably cast sourceValue to type T where sourceValue is bool and T is int? public static T ConvertTo<T>(Object sourceValue) { // IF IS OF THE SAME TYPE --> RETURN IMMEDIATELY if (sourceValue is T) return (T) sourceValue; var val = ConvertTo(sourceValue, typeof (T)); return (T) val; } Curren...

Cast LINQ result to ObservableCollection

The fact that it is a LINQ result might perhaps not be relevant for the question, but I'm mentioning it anyway - since this is the context which has resulted in this question. I run a LINQ query. The result is an; IEnumerable<MyClass> I want to put the result into an ObservableCollection; ObservableCollection<MyClass> How do I ...

Int cast error in generic extension

I modified the extension method Thorarin gave in response to this question to work on an int instead of a string: public static TEnum ToEnum<TEnum>(this int intEnumValue, TEnum defaultValue) { if (!Enum.IsDefined(typeof(TEnum), intEnumValue)) return defaultValue; return (TEnum)intEnumValue; } The compiler gives the err...

javac complains: cannot find symbol on enum implementing interface

I have three java types as defined below: Main.java: import java.util.Arrays; import java.util.List; public class Main { private Object callFunction() { OperationDefinitions func = OperationDefinitions.CONCATENATE; List<Object> values = Arrays.asList(new Object[] {"ABC", "-", "DEF"}); return func.call (values)...

What's going on in the 'offsetof' operator?

Visual C++ 2008 C runtime offers an operator 'offsetof', which is actually macro defined as this: #define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) This allows you to calculate the offset of the member variable m within the class s. What I don't understand in this declaration is: Why are we cas...

Is there ever an excuse for throwing an Exception from an implicit conversion?

From MSDN: By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose inf...

Why can't I set object derived from ObservableCollection<Proddata> equal to object returned as ObservableCollection<Proddata>?

I get a compile error, "Cannot implicitly conver type 'System.Collections.ObjectModel.ObservableCollection to ProddataRecsObservable'. An explicit conversion exists" See comments in following code segments. //I created a custom class called ProddataRecsObservable derived from //ObservableCollection<Proddata> so I can do some special CRU...