[ACCEPTED]-Can you pass a "type" as an argument?-types

Accepted answer
Score: 15

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

Function SomeFunction(Of T)(obj As Object) As T
    '' Magic
End Function
Score: 3

Great Answer - Here is a generic function 1 to do the same:

Public Sub BindListControlToEnum(Of T)(ListCtrl As ListControl)
    Dim itemValues As Array = System.Enum.GetValues(GetType(T))
    Dim itemNames As Array = System.Enum.GetNames(GetType(T))
    For i As Integer = 0 To itemNames.Length - 1
        Dim item As New ListItem(itemNames(i), itemValues(i))
End Sub

Call it like this:

BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
Score: 2

you want to use the

function task(of myType)(value as myType) as MyType
   return value
end function


Score: 0

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

Score: 0

I had to do something similar today (essentially 1 filling in the '' magic from the accepted answer):

Private Function Convert_Value_Or_Fallback(Of T)(ByRef value As Object, ByRef fallback As Object) As Object
        Return DirectCast(value, T)
    Catch ex As Exception
        Return fallback
    End Try
End Function

'call it like this:'
Convert_Value_Or_Fallback(Of Double)(value, 0)

More Related questions