Dynamic Typecasting

G

Guest

I have a variable and a data-type string stored in a database. I need to
dynamically cast the variable to the datatype at run-time. I am able to
create the Type from the data-type string, but I am not able to cast it
correctly. Here is an example of my code, although realize that I would need
to accomplish this with any type (Boolean, Guid, String, Integer...), and not
just GUID:

Dim typeStr as String = "System.GUID"
Dim myType As Type = Type.GetType(typeStr, True, True)
Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"

'this is the code that does not work. myType gets underlined in blue,
saying "Type 'tmpType' is not defined."
varStr = CType(varStr, myType)
 
C

Cor Ligthert[MVP]

Hi Honus,

Why you want to do it this, way, it sounds to me as.

I need a billion dollars tomorrow, can you tell me how to do that?

Cor
 
A

Armin Zingler

honus said:
I have a variable and a data-type string stored in a database. I
need to dynamically cast the variable to the datatype at run-time.
I am able to create the Type from the data-type string, but I am not
able to cast it correctly. Here is an example of my code, although
realize that I would need to accomplish this with any type (Boolean,
Guid, String, Integer...), and not just GUID:

Dim typeStr as String = "System.GUID"
Dim myType As Type = Type.GetType(typeStr, True, True)
Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"

'this is the code that does not work. myType gets underlined in
blue, saying "Type 'tmpType' is not defined."
varStr = CType(varStr, myType)

What you want to do is contradictive:
- Casting is done by the compiler. The compiler must know the destination
type.
- Your destination type is unknown at compile time, therefore it can not
work.


Armin
 
H

Herfried K. Wagner [MVP]

honus said:
I have a variable and a data-type string stored in a database. I need to
dynamically cast the variable to the datatype at run-time.

Casting is primarily a compile-time feature which will help the compiler to
detect certain flaws in the code.

If you are talking about type conversions, take a look at the
'TypeConverter' class.
 
C

Cor Ligthert[MVP]

Honus,
Your synnicism is refreshing and envigorating. Oh wait, never mind.
What you call this what you are now writing, informing us in a way that we
able to help you?

We are sharing information here, that is not in one direction, we try to
learn from each other.

Cor
 
P

Phill W.

honus said:
I have a variable and a data-type string stored in a database. I need to
dynamically cast the variable to the datatype at run-time. I am able to
create the Type from the data-type string, but I am not able to cast it
correctly.
Dim typeStr as String = "System.GUID"
Dim myType As Type = Type.GetType(typeStr, True, True)
Dim varStr as String = "81db359d-a970-4247-89f8-67995412da35"

All the data types have to be known at compile type, unless you resort
to some /really/ nasty P/Invoke-style coding (Yuk!).

Rather than that, since you will have the Type available at runtime, you
can use Activator.CreateInstance() to instantiate it:

Dim o As Object _
= Activator.CreateInstance( myType )

But, of course, "as Object" is /nowhere/ as useful "as Variant" used to
be... :)

HTH,
Phill W.
 
P

Patrice

If not yet solved your best bet is likely to explain what you are trying to
do as the code fragment you show doesn't make really sense (for example
casting a variable VarStr to another type and then affecting this variable
to the same variable doesn't make really sense).

You can create a guid using Dim g as new Guid(VarStr) and you can convert
the GUID back to text using the ToString method.
 

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