Late bound type conversion

Z

zdrakec

Hello all:

I have a list of values from a database table, each of which is
identified by name and by type, thus:

ValueField TypeName
---------- --------
"Value1" "String"
"Value2" "Boolean"
"Value3" "Integer"

In the application, these values are being put into an array of
objects, thus:

Dim obj() as Object = {"Value1","Value2","Value3"}

I would like to perform a type conversion on each object in the array,
without using a Case statement to do so. However, if I try something
like:

Dim lType as Type = Type.GetType("String")

I am not then able to do:

obj(0) = CType(obj(0),lType)

since the CType function requires an actual type, and will not accept a
variable name.

Suggestions?

Thanks much,
zdrakec
 
H

Herfried K. Wagner [MVP]

zdrakec said:
I am not then able to do:

obj(0) = CType(obj(0),lType)

since the CType function requires an actual type, and will not accept a
variable name.

Take a look at 'Convert.ChangeType'.
 
Z

zdrakec

Ah, what if the typename is actually an array of types, that is,
instead of "String", I have, "String()" ?

Thanks again,
zdrakec
 
H

Herfried K. Wagner [MVP]

zdrakec said:
Ah, what if the typename is actually an array of types, that is,
instead of "String", I have, "String()" ?

You'll have to iterate over the array's items and perform the conversion for
each item.
 

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