Example. First, this program calls VarType on an Integer. We print the String representation of the VariantType result. We show the GetType function, which is separate.
Finally We test the VariantType variable in an If-statement. We display a helpful message.
Dim value As Integer = 5
' Get VarType.
Dim type As VariantType = VarType(value)
' Write string representation.
' Show GetType method.
' You can check the VariantType against constants.If type = VariantType.Integer Then
Console.WriteLine("It's an integer!")
It's an integer!
Change. What happens if you change the "Dim value As Integer" line to "Dim value As String"? The result of the program will be different.
Also The If-statement will not evaluate to True and the final line will not be printed to the output.
Info VarType can be used on a variable of type Object. It resolves the Object variable's type to its most derived type (String).
Dim value As String = "cat"String
A discussion. What happens inside the VarType function? It calls the GetType function. Then it converts the result of GetType to a String representation.
Tip VarType is most useful when you must resolve the most derived type from an Object or other base type.
A summary. The VarType function is based on the GetType function. It will return the best type for variables such as Integers, which can simplify your VB.NET programs.