Runtime Type Casting .. need help
Posted on 2004-09-28
I have some code that looks like this:
Dim mytype As Type = Type.GetType(table.Columns(map.DataColumnName).DataType.ToString())
datarow.Item(map.DataColumnName) = CType(row(IntegerIndex(map.TextColumnIndex)), 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.TextColumnIndex))
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.TextColumnIndex)) = "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.