Home All Groups Group Topic Archive Search About

How to CType() when target type's name is in a string

Author
9 May 2006 8:42 PM
Don
Let's say I have a type name stored in a string (e.g. Dim typeName As String
= "Decimal").  Is it possible to CType() a variable to the type whose name
is stored in that string?

e.g.

Dim typeName As String = "Decimal"
Dim source as String = "3"
Dim target as Decimal

target = CType(source, <magic happens with typeName> )


Is there maybe another method that does this, instead of CType()?

Author
9 May 2006 8:52 PM
Mythran
"Don" <unkn***@oblivion.com> wrote in message
news:v878g.144537$WI1.23455@pd7tw2no...
Show quoteHide quote
> Let's say I have a type name stored in a string (e.g. Dim typeName As
> String = "Decimal").  Is it possible to CType() a variable to the type
> whose name is stored in that string?
>
> e.g.
>
> Dim typeName As String = "Decimal"
> Dim source as String = "3"
> Dim target as Decimal
>
> target = CType(source, <magic happens with typeName> )
>
>
> Is there maybe another method that does this, instead of CType()?
>

You might be able to using reflection, but when it's a known type, wouldn't
using "if" statements be faster?

HTH,
Mythran
Author
9 May 2006 9:24 PM
Don
Show quote Hide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:OJ571p6cGHA.3632@TK2MSFTNGP02.phx.gbl...
>
> "Don" <unkn***@oblivion.com> wrote in message
> news:v878g.144537$WI1.23455@pd7tw2no...
>> Let's say I have a type name stored in a string (e.g. Dim typeName As
>> String = "Decimal").  Is it possible to CType() a variable to the type
>> whose name is stored in that string?
>>
>> e.g.
>>
>> Dim typeName As String = "Decimal"
>> Dim source as String = "3"
>> Dim target as Decimal
>>
>> target = CType(source, <magic happens with typeName> )
>>
>>
>> Is there maybe another method that does this, instead of CType()?
>>
>
> You might be able to using reflection, but when it's a known type,
> wouldn't using "if" statements be faster?

The "known type" was just an example.
Author
9 May 2006 9:05 PM
Herfried K. Wagner [MVP]
"Don" <unkn***@oblivion.com> schrieb:
> Let's say I have a type name stored in a string (e.g. Dim typeName As
> String = "Decimal").  Is it possible to CType() a variable to the type
> whose name is stored in that string?
>
> e.g.
>
> Dim typeName As String = "Decimal"
> Dim source as String = "3"
> Dim target as Decimal
>
> target = CType(source, <magic happens with typeName> )

Performing a cast ('DirectCast') would not make much sense.  Its advantage
is type checking at design time, not at runtime.  However, you can use
'Convert.ChangeType' for converting values between types:

\\\
Dim b As Object = Convert.ChangeType(a, Type.GetType(...))
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
9 May 2006 9:24 PM
Don
Show quote Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:uxjEGx6cGHA.1436@TK2MSFTNGP05.phx.gbl...
> "Don" <unkn***@oblivion.com> schrieb:
>> Let's say I have a type name stored in a string (e.g. Dim typeName As
>> String = "Decimal").  Is it possible to CType() a variable to the type
>> whose name is stored in that string?
>>
>> e.g.
>>
>> Dim typeName As String = "Decimal"
>> Dim source as String = "3"
>> Dim target as Decimal
>>
>> target = CType(source, <magic happens with typeName> )
>
> Performing a cast ('DirectCast') would not make much sense.  Its advantage
> is type checking at design time, not at runtime.  However, you can use
> 'Convert.ChangeType' for converting values between types:
>
> \\\
> Dim b As Object = Convert.ChangeType(a, Type.GetType(...))
> ///

Thanks.  I'll look into Convert.ChangeType()

- Don