gregasm
asked on
Runtime Type Casting .. need help
I have some code that looks like this:
Dim mytype As Type = Type.GetType(table.Columns (map.DataC olumnName) .DataType. ToString() )
datarow.Item(map.DataColum nName) = CType(row(IntegerIndex(map .TextColum nIndex)), mytype)
But that doesn't work. It says "mytype" is not defined. I think the problem has to do with the fact that the CType method only works with design time values.
I need to cast at runtime this object: row(IntegerIndex(map.TextC olumnIndex ))
to a type that I will only know at runtime. The type that I want to cast it to is the underlying datatype of the strongly typed datatable.
Can someone help me? This code without the CType is blowing up when
row(IntegerIndex(map.TextC olumnIndex )) = "0"
and the datatable column type = "System.Boolean"
It seems this should be possible, and quite frankly, I am amazed that I am stuck on this.
Dim mytype As Type = Type.GetType(table.Columns
datarow.Item(map.DataColum
But that doesn't work. It says "mytype" is not defined. I think the problem has to do with the fact that the CType method only works with design time values.
I need to cast at runtime this object: row(IntegerIndex(map.TextC
to a type that I will only know at runtime. The type that I want to cast it to is the underlying datatype of the strongly typed datatable.
Can someone help me? This code without the CType is blowing up when
row(IntegerIndex(map.TextC
and the datatable column type = "System.Boolean"
It seems this should be possible, and quite frankly, I am amazed that I am stuck on this.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi wguerram,
The select case approach is what I ahve been working with.
can you throw out a skeleton of the select case for me to have a look at?
The select case approach is what I ahve been working with.
can you throw out a skeleton of the select case for me to have a look at?
ASKER
Yes, I think I tried that. The compiler doesn't like that expression though because it doesn't know the value at design time. Therefore, it kind of defeats one of the purposes of strong typing.
CType(row(IntegerIndex(map .TextColum nIndex)), GetType(mytype))
What is mytype? It's not known until runtime...
i have a feeling tomorrow i'm gonan write something like this:
select case table.Columns(map.DataColu mnName).Da taType.ToS tring()
case "System.Boolean"
.....
case "System.String"
case "System.Double"
case "This.Approach.Sucks"
case else
end select
CType(row(IntegerIndex(map
What is mytype? It's not known until runtime...
i have a feeling tomorrow i'm gonan write something like this:
select case table.Columns(map.DataColu
case "System.Boolean"
.....
case "System.String"
case "System.Double"
case "This.Approach.Sucks"
case else
end select
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oops. I meant to accept the answer from wguerram. But the points are distributed as I wanted them at least. Thanks guys.
You can change it
https://www.experts-exchange.com/help.jsp#hi17
Post a question with zero points and add the link to the question.
https://www.experts-exchange.com/help.jsp#hi17
Post a question with zero points and add the link to the question.
ASKER
Alrightie.
This is not the way to use CTYPE(), the second parameter is the name of the type, this garantee the compiler the type of object you are trying to convert can be converted.
If you are having problem with some types you have to use a "select case"
Although is weird you are getting an error with this:
row(IntegerIndex(map.TextC
and the datatable column type = "System.Boolean"
"0" is false and non zero is true, now if for some reason you assign "s", you will get an error.
But the compiler try to make the conversion to the new type if posible. otherwise you will get an error.
I don't recommend you to leave the compiler make the conversions, since you can get unexpected results in some types.