VB6Parse / Library / Objects / typename

VB6 Library Reference

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

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

Typical Uses

  1. Debugging variable types
  2. Logging type information
  3. Type checking in generic code
  4. Handling Variant variables
  5. Validating function arguments
  6. Reflection-like operations
  7. Error handling and reporting
  8. 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

Performance Notes

Best Practices

  1. Use for debugging and logging.
  2. Do not use for strict type enforcement.
  3. Handle "Nothing", "Null", and "Empty" cases.
  4. Use with Variant variables for type safety.
  5. Use for generic code and utilities.
  6. Document expected type strings.
  7. Use with arrays for type detection.
  8. Avoid using as a substitute for type declarations.
  9. Use for runtime checks, not compile-time.
  10. Combine with VarType for 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

Limitations

← Back to Objects | View all functions