verifying datatype convertibility programmatically

G

Guest

Hi,

I need a mechanism to verify programatically, if a particular datatype can
be/ can not be converted to another datatype with/without any loss of data.

i tried using TypeConverter.CanConvertFrom, but am getting incorrect values.

also i need the check to be done using strings containing the datatype names
eg., i need to know if any datatype of type "bool" can be converted to the
datatype "decimal", where i have the two strings stored in variables.

my failed attempt with TypeConverter.CanConvertFrom

TypeConverter x = TypeDescriptor.GetConverter( typeof( bool ));

bool b1 = x.CanConvertFrom( typeof( string ) );

bool b2 = x.CanConvertTo( typeof( string ) );

both b1 and b2 come out to be true !!!

Thanks in advance
regards
KumarForG
 
S

sadhu

A string can be converted to a bool and a bool can be converted to a
string. I think the TypeConverter does work correctly.
If you are looking for checking overflow, you can use "checked" blocks.
Within checked blocks, if a overflow occurs, an exception gets thrown.
You can then catch the exception and do whatever you want with it.
 
G

Guest

Hi Sadhu,

Thanks for the reply
but my question is,
while bool can always be converted to a string, a string can not always
be converted to a bool.

i need to get this kind of info programatically for any two given dotnet
datatypes.
is there a way to do so without explicitly maintining an in memory map of
what datatype can and what can not be converted?

regards
-KumarForG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top