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
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
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
you want to use the
function task(of myType)(value as myType) as MyType
''stuff
return value
end function
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
.