C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Finally: We test the VariantType variable in an If-statement. We display a helpful message.
VB.NET program that uses VarType function
Module Module1
Sub Main()
' Integer.
Dim value As Integer = 5
' Get VarType.
Dim type As VariantType = VarType(value)
' Write string representation.
Console.WriteLine(type.ToString())
' Show GetType method.
Console.WriteLine(value.GetType().ToString())
' You can check the VariantType against constants.
If type = VariantType.Integer Then
Console.WriteLine("It's an integer!")
End If
End Sub
End Module
Output
Integer
System.Int32
It's an integer!
Changed line in program: VB.NET
Dim value As String = "cat"
Output
String
System.String
New lines in program: VB.NET
' Integer.
Dim a As String = "cat"
' Cast to Object.
Dim value As Object = a
Output
String
System.String
Note: It is most useful when you must resolve the most derived type from an Object or other base type.