TypeName Function |
Microsoft.VisualBasic.Information
TypeName( varname )
Name of a variable
String
Returns a string giving data type information about varname . The possible return values are:
String returned | Variable contents |
---|---|
Boolean | 8-bit True or False value type |
Byte | 8-bit binary value type |
Char | 16-bit character value type |
Date | 64-bit date and time value type |
DBNull | Reference type indicating missing or nonexistent data |
Decimal | 96-bit fixed point numeric value type |
Double | 64-bit floating point numeric value type |
Error | Error object |
Integer | 32-bit integer value type |
Long | 64-bit integer value type |
Nothing | Object variable with no object currently assigned to it, uninitialized string, or undimensioned array |
Object | Reference type pointing to an unspecialized object |
Short | 16-bit integer value type |
Single | 32-bit floating point numeric value type |
String | Reference type pointing to a string of 16-bit characters |
< objectclass > | Reference type pointing to a specialized object created from class < objectclass > |
< structure > | A variable created from a structure or user -defined type named structure |
< typename >( ) | Dimensioned array |
If varname is declared as type Object, it returns the data subtype that has been assigned to it.
varname returns the data type name of all value types. It returns Nothing for uninitialized reference types and the data type name for all initialized reference types.
If varname is an array that has been initialized or dimensioned, the returned string will be the entry in the previous table corresponding to the underlying data type of the array, but with empty parentheses appended to the end of the name. For example, if varname points to an array of integers, TypeName returns Integer( ) .
When TypeName returns the name of a reference type, such as a class, it only returns the simple name, not the qualified name. For example, if varname points to an object of class System.Drawing.Printing.PaperSource, TypeName returns PaperSource .
If varname is of type Object, TypeName returns the data subtype stored to that object.
Dim obj As Object obj = New CEmployee( ) MsgBox(TypeName(obj)) ' Displays: CEmployee obj = 100 MsgBox(TypeName(obj)) ' Displays: Integer obj = Nothing MsgBox(TypeName(obj)) ' Displays: Nothing
The TypeName function also works directly with members of the Foundation Class Library that aren't wrapped by Visual Basic. It reports the following data types:
String returned | Variable contents |
---|---|
UINT16 | Unsigned 16-bit integer |
UINT32 | Unsigned 32-bit integer |
UINT64 | Unsigned 64-bit integer |
SBYTE | Signed byte |
In VB 6, the call to the TypeName function in the code fragment:
Dim strVar As String Console.WriteLine(TypeName(strVar))
returns a String. In VB.NET, the TypeName function in an equivalent code fragment returns Nothing . This is because in VB.NET, strings are reference types and reference types are implemented as objects.
In VB 6, passing a user-defined type to the TypeName function generates a compile error. In VB.NET, it returns the name of the user-defined type or structure.
In VB 6, passing an uninitialized array to the TypeName function returns the type name plus parentheses. In VB.NET, it returns Nothing .
In VB 6, a variable whose type is not declared is reported as a Variant; in VB . NET, it is an object.
VarType Function