casting

VB.NET Conversion problem when trying to convert from base class to subclass (BC30311: "Value of type '<type1>' cannot be converted to '<type2>'")

Hello! Please take a look at the following code: Public Sub Method(Of TEntity As EntityObject)(ByVal entity As TEntity) If TypeOf entity Is Contact Then Dim contact As Contact = entity 'Compile error' Dim contact = DirectCast(entity, Contact) 'Compile error' Dim contact = CType(entity, Contact) 'Compile er...

Getting cannot convert object to ArrayList error when retrieving ArrayList from HttpSession

I have saved an ArrayList to the session object. I am trying to retrieve it using sriList = session.getAttribute("scannedMatches"); I am getting the compile time error "Cannot convert from Object to ArrayList". How can I get my ArrayList back from the session object. ...

Casting ints to enums in C#

There is something that I cannot understand in C#. You can cast an out-of-range int into an enum and the compiler does not flinch. Imagine this enum: enum Colour { Red = 1, Green = 2, Blue = 3 } Now, if you write: Colour eco; eco = (Colour)17; The compiler thinks that’s fine. And the runtime, too. Uh? Why did the C# te...

Casting from Object in Java without getting an unchecked warning

I wrote a class that has a map of <String, Object>. I need it to hold arbitrary objects, but at the same time sometimes I need to cast some of those objects, so I'll do something like HashMap<String, Object> map = new HashMap<String, Object>(); Object foo =...

In C#, is it possible to cast a List<Child> to List<Parent>?

I want to do something like this: List<Child> childList = new List<Child>(); ... List<Parent> parentList = childList; However, because parentList is a List of Child's ancestor, rather than a direct ancestor, I am unable to do this. Is there workaround (other than adding each element individually)? ...

How to use generic class (or interface) in another generic class (or interface) with the same data type parameters without casting

I need the Cache class that keep the <TKey key, TValue value> pairs. And it is desirable that TKey can be any class that supports Serializable interface and TValue can be any class that supports Serializable and my own ICacheable interfaces. There is another CacheItem class that keeps <TKey key, TValue value> pair. I want the Cache cla...

Structure Saved in a Session Variable

The ReportInfo is a structure. The structure works fine on one web page but I am trying to use it on another web-page. Here is where I saved the ReportInfo structure to the Session Variable Session["ReportInfo"] = reportInfo; On the other web-page, I re-created the Structure and then assign the session variable to it, like this... r...

Casting objects in JRuby

Is there a way I can explicitly cast one Java object to another Java class from JRuby? Sometimes I want to be able to invoke SomeJavaClass#aMethod(MySuperClass) rather than SomeJavaClass#aMethod(MyClass) from JRuby. From Java, I'd do this: someJavaObject.aMethod( (MySuperClass) myObj ); but I didn't see a #cast ruby method or an...

why downcast fails at runtime

Hi All, I want to know why below downcast fails @ run time: case 1: Object y = 10.23; Console.WriteLine(y.GetType()); //System.Double int z = (int)y;// fails @ runtime Console.ReadKey(); case 2: enter code here Double y = 10.23; Console.Wr...

How to check if implicit or explicit cast exists?

I have a generic class and I want to enforce that instances of the type parameter are always "cast-able" / convertible from String. Is it possible to do this without for example using an interface? Possible implementation: public class MyClass<T> where T : IConvertibleFrom<string>, new() { public T DoSomethingWith(string s) { ...

What's the cost of "as" compared to QueryInterface in COM or dynamic_cast in C++?

I'm still trying to map my deep and old knowledge from C/C++ to my somewhat more shallow .Net knowledge. Today the time has come to "as" (and implicitly "is" and cast) in C#. My mental model of "as" is that it's a QueryInterface or dynamic_cast (a dynamic_cast with pointer argument, not reference, that is) for C#. My question is two-fol...

C++ type casting vector class

I have two vector classes: typedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT z; } D3DXVECTOR3, *LPD3DXVECTOR3; and class MyVector3{ FLOAT x; FLOAT y; FLOAT z; }; and a function: void function(D3DXVECTOR3* Vector); How is it possible (if it's possible) to achieve something like this: MyVector3 vTest; f...

c function merge help

I have two functions: void free_this(THIS *this) { THIS *this_tmp; while (this_tmp = this) { if (this->str) free(this->str); this = this_tmp->next; free(this_tmp); } } void free_that(THAT *that) { THAT *that_tmp; while (that_tmp = that) { if (that->id) ...

C# Type Casting at Run-Time Using Reflection

Judging by the title of this question, what I want to do may not be possible so I will describe what I'm doing and you can feel free to let me know what I'm doing wrong and what would be a better way to accomplish my goal. I have an XML file that describes 1) a custom object that is derived of a base type, and 2) the internal field name...

How to cast an object programmatically at runtime?

Suppose I have a custom control like: MyControl : Control if I have code like this: List<Control> list = new ... list.Add (myControl); RolloutAnimation anim = list[0]; I know I can do it at compile time but I wanna do it at runtime and access MyControl specific functionality. ...

C++: Safe way to cast an integer to a pointer

I need to convert an integral type which contains an address to the actual pointer type. I could use reinterpret_cast as follows: MyClass *mc1 = reinterpret_cast<MyClass*>(the_integer); However, this does not perform any run-time checks to see if the address in question actually holds a MyClass object. I want to know if there is any b...

In SQL Server change column of type int to type text

I would like to change a column in SQL Server from type int, to type text, while maintaining the column name. The table with this column has lots of data, and I do not want to lose it. SQL Server doesn't seem to support implicit or explicit casts from int to text, otherwise this would be pretty simple. So, how would you do it using on...

Converting a .NET object to (byte*)

Is there a way to cast a System.Object to byte*? ...

C# - Can a List<MyClass> be seemlessly cast to a List<Interface> or similar?

I have a DataSource in my control which is always a List<T> where T has to inherit from IEntity. public class MyClass<T> where T : IEntity { public List<T> DataSource { get; set; } } Now, obviously you can't cast a List<T> to a List<IEntity> doing the following: List<IEntity> wontWork = (List<IEntity>)this...

Tracking instances with generics and supporting subclasses

I've defined the following generic class public class ManagedClass<T> where T : ManagedClass<T> { static ManagedClass() { Manager = new ObjectManager<T>(); } public static ObjectManager<T> Manager { get; protected set; } public ManagedClass() { Manager.Add( (T)this ); } } The idea is that I...