directcast to a user Type

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
Is it possible to do:


dim SomeVar as Integer

Dim theType As Type = SomeVar.GetType

dim Test as Object

Test = SomeVar

dim myVar = DirectCast(Test, theType) -> compilation error, what is the
work around ?

Thx
 
Sam said:
dim SomeVar as Integer

Dim theType As Type = SomeVar.GetType

dim Test as Object

Test = SomeVar

dim myVar = DirectCast(Test, theType) -> compilation error, what is the
work around ?

The second parameter must be known at compile-time. This means that you
cannot pass a 'Type' object to this parameter at runtime. In this
particular case casting would not make sense because you are assigning the
result of the cast to a variable of type 'Object', which is the base class
of all classes.
 
Herfried,

You took not that much time as I took.

In my opinion was the OP asking.

How can I assign a variable to a type, while I only know that it has to be
the same as another type that I know the name from.

As that it is in my opinion a theroatical question for what I cannot see any
use, did I stop with thinking about it.

However I know that you "sometimes" like this kind of questions.

:-)

Cor
 

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

Back
Top