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
The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method.
You can't. You can only pass a value, and CustomStruct is not a value but a type. Using a type identifier is a compile-time error. (Also don't forget that this returns an empty string for anonymous types such as []int .)
Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.
The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: C# Copy.
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
Great Answer - Here is a generic function 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))
ListCtrl.Items.Add(item)
Next
End Sub
Call it like this:
BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With