|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to CType() when target type's name is in a stringLet'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()? "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 You might be able to using reflection, but when it's a known type, wouldn't > 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()? > using "if" statements be faster? HTH, Mythran
Show quote
Hide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message The "known type" was just an example.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? "Don" <unkn***@oblivion.com> schrieb: Performing a cast ('DirectCast') would not make much sense. Its advantage > 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 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/>
Show quote
Hide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message Thanks. I'll look into Convert.ChangeType()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(...)) > /// - Don
Saving Blob Data To File
How to disable the numericupdown event firing? Question en programmation ! Need to lock read a text file, then delete... Dataset or SQL? wich is faster? Check if Service is running? How to interrupt a running program How to kill a terminal server session (VB.NET) Safe / Unsafe Native Methods Create System.Console from a winforms app |
|||||||||||||||||||||||