colepc
asked on
VB.NET - converting datatypes in code
Hi All,
I'm needing to dynamically convert datatype from a string representation of SqlDbType (like "nchar" for SqlDbType.NChar) to a System.Type in code.
For example, I want to store a System.Type with a based on some value, like:
dim mytype as System.Type
If myvar = "nchar" then mytype = System.Type.GetType("Syste m.String")
I want to then use mytype to convert some piece of data, like:
dim myobject as Object
myobject = CType(somevalue, mytype)
The CType() fails, however, with an excption saying that "Type 'mytype' is not defined."
The app that this applies to is a data conversion app where I'm iterating through rows and columns converting them to another vendors' application.
My question is, can what I'm wanting to do be done?
Thanks,
Terry
I'm needing to dynamically convert datatype from a string representation of SqlDbType (like "nchar" for SqlDbType.NChar) to a System.Type in code.
For example, I want to store a System.Type with a based on some value, like:
dim mytype as System.Type
If myvar = "nchar" then mytype = System.Type.GetType("Syste
I want to then use mytype to convert some piece of data, like:
dim myobject as Object
myobject = CType(somevalue, mytype)
The CType() fails, however, with an excption saying that "Type 'mytype' is not defined."
The app that this applies to is a data conversion app where I'm iterating through rows and columns converting them to another vendors' application.
My question is, can what I'm wanting to do be done?
Thanks,
Terry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Public Function Convert(objData as object, strCurrentType as string) as Object
Select Case strCurrentType
Case "nchar"
Return CType(objData, String)
Case "bigint"
Return CType(objData, Int64)
...
End Case
This works, but I'm trying to speed it up as much as possible. I'd like to put this same functionaltiy into a hashtable to produce the same result.
Thanks in advance!