Casting in VB.NET
Asked Answered
T

5

14

I would like to be able to cast a value dynamically where the type is known only at runtime. Something like this:

myvalue = CType(value, "String, Integer or Boolean")

The string that contains the type value is passed as an argument and is also read from a database, and the value is stored as string in the database.

Is this possible?

Toothed answered 30/10, 2008 at 19:42 Comment(0)
S
7

Sure, but myvalue will have to be defined as of type Object, and you don't necessarily want that. Perhaps this is a case better served by generics.

What determines what type will be used?

Sudbury answered 30/10, 2008 at 19:49 Comment(8)
just a string indicating the type "String", "integer", "boolean"Toothed
the string is passed as argumentToothed
and the value is stored as string in the DBToothed
codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx .. that's all I found on the topicPastiche
I selected this answer because generics seems to be more abstract and global for other types. Other answers were also valid from tom.dietrich and from Atkinson . Thanks Everybody for your help.Toothed
@Youssef: What will you do with MyValue once you have it? I could easily to write a method that SELECTs(hint) the right cast, but it would have to accept and return an object, so it wouldn't be much use.Sudbury
Joel: myvalue is used as a settings to be checked in some of my business functions. like this: can_Do_something=Settings.GetValue(of Boolean) (UserId, "aSettingKey") if can_Do_something then.. in other places I use it like this Max_value= Settings.GetValue(of Integer) (UserId, "aSettingKey")Toothed
Okay. In that case, it looks like the code that uses each setting knows the value at compile time. That makes things much simpler (as it looks like you're finding out).Sudbury
S
9
 Dim bMyValue As Boolean
 Dim iMyValue As Integer
 Dim sMyValue As String 
 Dim t As Type = myValue.GetType


 Select Case t.Name
     Case "String"
        sMyValue = ctype(myValue, string)
     Case "Boolean"
        bMyValue = ctype(myValue, boolean)
     Case "Integer"
        iMyValue = ctype(myValue, Integer)
 End Select

It's a bit hacky but it works.

Statius answered 30/10, 2008 at 20:14 Comment(0)
S
7

Sure, but myvalue will have to be defined as of type Object, and you don't necessarily want that. Perhaps this is a case better served by generics.

What determines what type will be used?

Sudbury answered 30/10, 2008 at 19:49 Comment(8)
just a string indicating the type "String", "integer", "boolean"Toothed
the string is passed as argumentToothed
and the value is stored as string in the DBToothed
codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx .. that's all I found on the topicPastiche
I selected this answer because generics seems to be more abstract and global for other types. Other answers were also valid from tom.dietrich and from Atkinson . Thanks Everybody for your help.Toothed
@Youssef: What will you do with MyValue once you have it? I could easily to write a method that SELECTs(hint) the right cast, but it would have to accept and return an object, so it wouldn't be much use.Sudbury
Joel: myvalue is used as a settings to be checked in some of my business functions. like this: can_Do_something=Settings.GetValue(of Boolean) (UserId, "aSettingKey") if can_Do_something then.. in other places I use it like this Max_value= Settings.GetValue(of Integer) (UserId, "aSettingKey")Toothed
Okay. In that case, it looks like the code that uses each setting knows the value at compile time. That makes things much simpler (as it looks like you're finding out).Sudbury
T
5

This is the shortest way to do it. I've tested it with multiple types.

Sub DoCast(ByVal something As Object)

    Dim newSomething = Convert.ChangeType(something, something.GetType())

End Sub
Tabbitha answered 30/10, 2008 at 20:10 Comment(0)
S
4

Well, how do you determine which type is required? As Joel said, this is probably a case for generics. The thing is: since you don't know the type at compile time, you can't treat the value returned anyway so casting doesn't really make sense here.

Synthetic answered 30/10, 2008 at 19:52 Comment(0)
A
2

Maybe instead of dynamically casting something (which doesn't seem to work) you could use reflection instead. It is easy enough to get and invoke specific methods or properties.

Dim t As Type = testObject.GetType()
Dim prop As PropertyInfo = t.GetProperty("propertyName")
Dim gmi As MethodInfo = prop.GetGetMethod()
gmi.Invoke(testObject, Nothing)

It isn't pretty but you could do some of that in one line instead of so many.

Aryan answered 30/10, 2008 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.