casting

Casting to abstract class or interface when generics are used

I have this method Verify_X which is called during databind for a listbox selected value. The problem is the strongly typed datasource. I want to use the abstract class BaseDataSource or an interface to call the methods supported: Parameters[] and Select(), Instead of using the most specific implementation as seen below. This is so on...

Is possible to cast a variable to a type stored in another variable?

This is what I need to do: object foo = GetFoo(); Type t = typeof(BarType); (foo as t).FunctionThatExistsInBarType(); Can something like this be done? ...

Treat a class as if it was defined with an interface

I have a 100 classes that have some similar elements and some unique. I've created an interface that names those similar items eg: interface IAnimal. What i would normally do is: class dog : IAnimal But there are 100 classes and i don't feel like going though them all and looking for the ones that i can apply IAnimal to. What i want ...

static_cast<int>(foo) vs. (int)foo

Could somebody please elaborate on the differences? ...

reinterpret_cast in C#

Hi I'm looking for a way to reinterpret an array of type byte[] as a different type, say short[]. In C++ this would be achieved by a simple cast but in C# I haven't found a way to achieve this without resorting to duplicating the entire buffer. Any ideas? ...

Casting to Unknown Type When Only Given Class Name as a String of That Type

I currently posses a List of Objects(Using Java 1.3), and let's say that I wanted to cast one of the Objects returned from list.get(i) to a type of which I only know the name of the Class as a String. Essentially, how do I Object o = (classname)list.get(i); where className is a String variable of a className. I thought that I could use...

C# - Collections and casting

I have a base class, BaseObject, and a number of classes which derive from BaseObject (DerivedObject1, DerivedObject2 etc) I have a collection of BaseObjects in which I storing the derived objects. The code I use to do this is: // declaring and initializing the idictionary public IDictionary<ulong, BaseObject> ObjectList = new Dictiona...

Asp.net membership error

We have a live running business system with an error which occurs from time to time. The error is according to the stack trace comming from the asp.net membership provider. It does not seem to matter which user is currently logged on. As far as i can tell the asp.net membership tables in the database are fine. Example of a code row gene...

Inheriting a Linq to SQL class and cast the result of a linq query

I am writing an application where we will need to extend a basic entity into a number of different things (eg employee, vehicle etc). The design is as such that there is a Entity table and a second table with type specific values eg an employee will have an ID Number but a vehicle will have a registration number. I have inherited from ...

Inheritance and casting in Java

Hello, I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes: public class Automobile { public int var; public Automobile () { var = 1; } public String toString () { return "AUTOMOBILE: " + var; } } ...

casting vs using the 'as' keyword in the CLR

I'm learning about design patterns and because of that I've ended using a lot of interfaces. One of my "goals" is to program to an interface, not an implementation. What I've found is that I'm doing a lot of casting or object type conversion. What I'd like to know is if there is a difference between these two methods of conversion: pu...

Casting reference variable in Java

Hello, I have something unclear concerning casting reference variable in Java. I have two classes A and B. A is the super class of B. If I have the two objects, and then the print statement: A a = new A(); //superclass B b = new B(); //subclass System.out.println ((A)b); then what exactly is happening when the println method is e...

Java: What's the difference between autoboxing and casting?

This question is about "Why does autoboxing make some calls ambiguous in Java?" But reading through the answers, there are a number of references to casting and I'm not sure I completely understand the difference. Can someone provide a simple explanation? ...

LPTSTR to int (c++)

Hi, I'm passing some numeric arguments while creating a process (in VC++) I'm stuck at casting LPTSTR to int. Thanks in advance, ...

Casting to Unknown Type When Class Name as a String

public class ExampleClass { public static void main(String[] args) { // TODO Auto-generated method stub Horse hr1 = new Horse(); Horse hr2 = new Horse(); Horse hr3 = new Horse(); Horse hr4 = new Horse(); Set hrSet = new HashSet(); hrSet.add(hr1); hrSet.add(hr2); hrSet.add(hr3); hrSet....

Howto cast pointer to generic parameter type?

Hello everybody, it's my first question here, glad to have found this site. My question deals with the new Generics feature in Delphi 2009. Basically I tried to write a generic wrapper class for an existing hash map implementation. The existing implementation stores (String, Pointer) pairs, so in the wrapper class I have to cast between...

C# specifying generic collection type param at runtime

I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do: Type t = typeof(Car); List<t> Cars = GetAll<t>(); How can I do this? I want to return a generic collection from the database of a type that I discover at runtime using reflection. ...

Handling overflow when casting doubles to integers in C

Today, I noticed that when I cast a double that is greater than the maximum possible integer to an integer, I get -2147483648. Similarly, when I cast a double that is less than the minimum possible integer, I also get -2147483648. Is this behavior defined for all platforms? What is the best way to detect this under/overflow? Is put...

Cast object to decimal? (nullable decimal)

If have this in the setter of a property: decimal? temp = value as decimal?; value = "90" But after the cast, temp is null... What is the proper way to do this cast? ...

Is it OK to use C-style cast for built-in types?

After reading here a lot of answers about C-style casting in C++ I still have one little question. Can I use C-style casting for built-in types like long x=(long)y; or it's still considered bad and dangerous? ...