VB6 TypeName Function
The TypeName function returns a string that provides information about the data type of a variable or expression.
Syntax
TypeName(varname)
Parameters
varname: Required. Name of a variable or expression whose data type is to be determined.
Returns
Returns a String describing the data type of the variable or expression. Common return values include:
- "Boolean"
- "Byte"
- "Integer"
- "Long"
- "Single"
- "Double"
- "Currency"
- "Date"
- "String"
- "Object"
- "Error"
- "Empty"
- "Null"
- "Nothing"
- "Variant"
- "Unknown"
- Custom class or user-defined type names
Remarks
- Returns the type as a string, not the actual type.
- For objects, returns the class name or interface name.
- For arrays, returns the base type name with "()" appended (e.g., "
Integer()", "String()"). - For objects not instantiated, returns "Nothing".
- For Null, returns "Null"; for Empty, returns "Empty".
- For user-defined types, returns the type name.
- For Variant variables, returns the underlying type.
- Useful for debugging, logging, and type checking at runtime.
- Not case-sensitive.
Typical Uses
- Debugging variable types
- Logging type information
- Type checking in generic code
- Handling Variant variables
- Validating function arguments
- Reflection-like operations
- Error handling and reporting
- Determining array types
Basic Examples
Example 1: Get type of variable
Dim x As Integer
MsgBox TypeName(x) ' "Integer"
Example 2: Get type of object
Dim c As Collection
Set c = New Collection
MsgBox TypeName(c) ' "Collection"
Example 3: Get type of array
Dim arr(1 To 5) As String
MsgBox TypeName(arr) ' "String()"
Example 4: Get type of Variant
Dim v As Variant
v = 123
MsgBox TypeName(v) ' "Integer"
Common Patterns
Pattern 1: Check for array
If Right$(TypeName(var), 2) = "()" Then
MsgBox "It's an array!"
End If
Pattern 2: Check for object
If TypeName(obj) = "Nothing" Then
MsgBox "Object not set!"
End If
Pattern 3: Handle Variant types
If TypeName(v) = "String" Then
' Handle string
End If
Pattern 4: Log variable types
Debug.Print "Type: " & TypeName(x)
Pattern 5: Validate argument type
Sub Foo(arg As Variant)
If TypeName(arg) <> "String" Then Err.Raise 5
End Sub
Pattern 6: Reflection-like usage
Dim t As String
t = TypeName(obj)
If t = "MyClass" Then
' Do something
End If
Pattern 7: Handle Null and Empty
If TypeName(v) = "Null" Then
' Handle Null
ElseIf TypeName(v) = "Empty" Then
' Handle Empty
End If
Pattern 8: Array type detection
If InStr(TypeName(arr), "()") > 0 Then
Debug.Print "Array of type: " & Left$(TypeName(arr), Len(TypeName(arr)) - 2)
End If
Pattern 9: User-defined type
Type MyType
x As Integer
End Type
Dim t As MyType
MsgBox TypeName(t) ' "MyType"
Pattern 10: Class type detection
If TypeName(obj) = "MyClass" Then
' Handle MyClass
End If
Advanced Usage
Example 1: Type checking in generic function
Function IsString(val As Variant) As Boolean
IsString = (TypeName(val) = "String")
End Function
Example 2: Logging all argument types
Sub LogTypes(ParamArray args() As Variant)
Dim i As Integer
For i = LBound(args) To UBound(args)
Debug.Print "Arg " & i & ": " & TypeName(args(i))
Next i
End Sub
Example 3: Reflection for class methods
If TypeName(obj) = "MyClass" Then
obj.SpecialMethod
End If
Example 4: Variant array detection
Dim v As Variant
v = Array(1, 2, 3)
If Right$(TypeName(v), 2) = "()" Then
Debug.Print "Variant array"
End If
Error Handling
- Returns "Unknown" for unsupported types.
- Returns "Nothing" for uninitialized object variables.
- Returns "Null" for Null values.
- Returns "Empty" for uninitialized variables.
Performance Notes
- Fast, constant time O(1).
- No side effects.
Best Practices
- Use for debugging and logging.
- Do not use for strict type enforcement.
- Handle "Nothing", "Null", and "Empty" cases.
- Use with Variant variables for type safety.
- Use for generic code and utilities.
- Document expected type strings.
- Use with arrays for type detection.
- Avoid using as a substitute for type declarations.
- Use for runtime checks, not compile-time.
- Combine with
VarTypefor more detail.
Comparison Table
| Function | Purpose | Input | Returns |
|---|---|---|---|
TypeName |
Get type as string | variable | String |
VarType |
Get type as constant | variable | Integer |
IsObject |
Check if is object | variable | Boolean |
IsArray |
Check if is array | variable | Boolean |
Platform Notes
- Available in VB6, VBA,
VBScript - Consistent across platforms
- Returns type names in English
Limitations
- Returns only type name as string
- Not locale-sensitive
- Returns "Unknown" for unsupported types
- Not for compile-time type checking
- May return user-defined type/class names