views:

145

answers:

3

I want to do something like the following in VB.NET, is it possible?

Function task(value as Object, toType as Type)

   Return DirectCast(value, toType)

End Function
+7  A: 

Yes. There is System.Type. You may actually want to do a Generic however.

Function SomeFunction(Of T)(obj As Object) As T
    '' Magic
End Function
Daniel A. White
Oh doh. I'm so used to C# these days.
Daniel A. White
+1  A: 

you want to use the

function task(of myType)(value as myType) as MyType
   ''stuff
   return value
end function
Fredou
A: 

Yes, though, depending on your requirements, you may want to use CType to do any type casting/conversion. CType will work so long as there is a valid type conversion, whereas DirectCast requires that value be of the type toType.

James Conigliaro