views:

724

answers:

5
+4  Q: 

Casting in VB.Net

I would like to be able to cast a value dynamically where the type is known only on runtime.

something like this

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

the string that contains the type value is passed as argument and also read from DB.

And the value is stored as string in the DB.

Is this possible ?

Thanks in advance.

+4  A: 

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?

Joel Coehoorn
just a string indicating the type "String", "integer", "boolean"
Youssef
Then what sets the string?
Joel Coehoorn
the string is passed as argument
Youssef
and the value is stored as string in the DB
Youssef
http://www.codeproject.com/KB/dotnet/CheatSheetCastingNET.aspx .. that's all I found on the topic
Tigraine
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.
Youssef
@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.
Joel Coehoorn
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")
Youssef
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).
Joel Coehoorn
+3  A: 

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.

Konrad Rudolph
+2  A: 

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.

Sam Corder
+3  A: 

This is the shortest way to do it. Ive tested it with multiple types.

Sub DoCast(ByVal something As Object)

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

End Sub
JTA
+3  A: 
 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.

tom.dietrich