views:

69

answers:

4

How would I go about the following... I have a control that can be bound to different data types... String, Int, Int32, DateTime, etc... but generically the result is stored into a generic "object" data type. So, I use another field to identify the EXPECTED type such as..

String BoundDataType = "System.String"   // or System.Int32 or date/time, etc.
object ChosenValue;

For comparison purposes, I would now have to enforce typecasting of expected format, such as

(DataBoundType)ChosenValue == (DataBoundType)TestAgainstThisValue;

I know i could put inside a switch, or overloaded functions with different Signatures per data type, but looking for a more generic way to handle directly.

Thanks

+4  A: 

You don't actually need a separate BoundDataType property - object.GetType() will suffice.

As for comparison, most standard types implement IComparable interface, which can be used to test for equality.

Anton Gogolev
A: 

Use the System.ComponentModel.TypeConverter-Class

J. Random Coder
A: 

Try

TestAgainstThisValue.GetType()

to get the type of the variable

Himadri
A: 

you can use object.GetType() to get the type of the variable.

Then you can use Convert.ChangeType(object,type) to make the conversion.

object conv = Convert.ChangeType(ChosenValue,ChosenValue.GetType());

this should work.

Alastair Pitts