54

How do I get the type of a variable using VBScript?

Daniel Imms
  • 43,032
  • 14
  • 130
  • 152
Ash Burlaczenko
  • 21,581
  • 14
  • 63
  • 95

6 Answers6

73

Is VarType what you need?

Returns a value indicating the subtype of a variable.

+--------------+-------+---------------------------------------------+
|   Constant   | Value |                 Description                 |
+--------------+-------+---------------------------------------------+
| vbEmpty      |     0 | Empty (uninitialized)                       |
| vbNull       |     1 | Null (no valid data)                        |
| vbInteger    |     2 | Integer                                     |
| vbLong       |     3 | Long integer                                |
| vbSingle     |     4 | Single-precision floating-point number      |
| vbDouble     |     5 | Double-precision floating-point number      |
| vbCurrency   |     6 | Currency                                    |
| vbDate       |     7 | Date                                        |
| vbString     |     8 | String                                      |
| vbObject     |     9 | Automation object                           |
| vbError      |    10 | Error                                       |
| vbBoolean    |    11 | Boolean                                     |
| vbVariant    |    12 | Variant (used only with arrays of Variants) |
| vbDataObject |    13 | A data-access object                        |
| vbDecimal    |    14 | Decimal Value                               |
| vbByte       |    17 | Byte                                        |
| vbLongLong   |    20 | LongLong integer (64 bit)                   |
| vbArray      |  8192 | Array                                       |
+--------------+-------+---------------------------------------------+

The VarType function never returns the value for Array by itself. It is always added to some other value to indicate an array of a particular type. The value for Variant is only returned when it has been added to the value for Array to indicate that the argument to the VarType function is an array. For example, the value returned for an array of integers is calculated as 2 + 8192, or 8194. If an object has a default property, VarType (object) returns the type of its default property.

Martin Smith
  • 402,107
  • 79
  • 682
  • 775
  • 1
    VarType(index) returns 8. What does this mean. – Ash Burlaczenko Jul 19 '10 at 13:27
  • 5
    8 = vbString - Indicates a string (From the link in my answer) – Martin Smith Jul 19 '10 at 13:30
  • 2
    Cheers. Common sense should have said to check that before asking. Sorry. +Rep. – Ash Burlaczenko Jul 19 '10 at 13:34
  • where to check which number stands for what? – Black Nov 18 '15 at 20:41
  • 1
    @EdwardBlack the linked page on `vartype` has the mapping. – Martin Smith Nov 18 '15 at 20:44
  • 1
    and type 14 is vbDecimal, which is missing from a of of documentation for some reason – Greg Woods Apr 05 '16 at 11:40
  • 2
    Note that not all dialects of VB support all types. In the case of VBScript (as asked in the question), only 0 through 9, 11 and 17 seem to be used. And arrays apparently always return 8204 ("array of variant"), no matter what values you put in them. – Mr Lister Dec 07 '17 at 11:28
  • This answer is partly wrong for Asp Classic. The linked reference is for VBA. The right reference is [this one](https://docs.microsoft.com/en-us/previous-versions//3kfz157h%28v%3dvs.85%29). Note that `vbLongLong`, `vbDecimal`, are not in it: they are not supported in Classic Asp VBScript as far as I know. (I was intending to just edit this answer but I realized that would have been a rollback of the author own edits.) – Frédéric Mar 29 '19 at 10:55
28

If you want to get the type name of an object assigned to a variable with Set, you can use TypeName instead.

Class SomeClass
    '' empty class
End Class

Dim x
Set x = New SomeClass
WScript.Echo TypeName(x)  '' displays "SomeClass"
Tmdean
  • 8,770
  • 40
  • 51
7

Tmdean's answer also works to get the Type Name (not an integer) of (almost) all other types of variables (per http://msdn.microsoft.com/en-us/library/ie/y58s1cs6%28v=vs.84%29.aspx)

dim i,s,a
i = 1
s = "Hello world"
a = split("Hello World"," ")

WScript.Echo TypeName(i) 'Displays "Integer"
WScript.Echo TypeName(s) 'Displays "String"
WScript.Echo TypeName(a) 'Displays "Variant()"
Community
  • 1
  • 1
user66001
  • 696
  • 1
  • 12
  • 34
3
Dim a, b, c, d, e, f
a = 10
b = "text"
c = Split("John Doe,Jane Smith,Dick Tracy", ",")
d = 1.2
e = Null
f = True
MsgBox "'a' is " & fVarType(a) & vbNewLine & _
       "'b' is " & fVarType(b) & vbNewLine & _
       "'c' is " & fVarType(c) & vbNewLine & _
       "'d' is " & fVarType(d) & vbNewLine & _
       "'e' is " & fVarType(e) & vbNewLine & _
       "'f' is " & fVarType(f) & vbNewLine & _
       "'g' is " & fVarType(c(0))

Function fVarType(x)
    Const ArrayCode = 8192
    Dim y
    y = VarType(x)
    If y < ArrayCode Then
        fVarType = fGetType(VarType(x))
    Else
        fVarType = "vbArray with " & fGetType(VarType(x)- ArrayCode) & " elements"
    End If  
End Function

Function fGetType(vType)
    Select Case vType   
        Case 0 fGetType = "vbEmpty"
        Case 1 fGetType = "vbNull"
        Case 2 fGetType = "vbInteger"
        Case 3 fGetType = "vbLong"
        Case 4 fGetType = "vbSingle"
        Case 5 fGetType = "vbDouble"
        Case 6 fGetType = "vbCurrency"
        Case 7 fGetType = "vbDate"
        Case 8 fGetType = "vbString"
        Case 9 fGetType = "vbObject"
        Case 10 fGetType = "vbError"
        Case 11 fGetType = "vbBoolean"
        Case 12 fGetType = "vbVariant"
        Case 13 fGetType = "vbDataObject"
        Case 14 fGetType = "vbDecimal"
        Case 17 fGetType = "vbByte"
        Case Else fGetType = "undetected"
    End Select
End Function
user66001
  • 696
  • 1
  • 12
  • 34
1

vartype is appropriate ...

Dim x
x=123
msgbox VarType(x)
user887648
  • 29
  • 1
0
Dim TypeDictionary

Set TypeDictionary = CreateObject("Scripting.Dictionary")
TypeDictionary.Add 0, "vbEmpty"
TypeDictionary.Add 1, "vbNull"
TypeDictionary.Add 2, "vbInteger"
TypeDictionary.Add 3, "vbLong"
TypeDictionary.Add 4, "vbSingle"
TypeDictionary.Add 5, "vbDouble"
TypeDictionary.Add 6, "vbCurrency"
TypeDictionary.Add 7, "vbDate"
TypeDictionary.Add 8, "vbString"
TypeDictionary.Add 9, "vbObject"
TypeDictionary.Add 10, "vbError"
TypeDictionary.Add 11, "vbBoolean"
TypeDictionary.Add 12, "vbVariant"
TypeDictionary.Add 13, "vbDataObject"
TypeDictionary.Add 17, "vbByte"

Public Function GetType(argument)
    GetType = TypeDictionary.Item(VarType(argument))
End Function

This version invests more effort setting up the dictionary, but then looks up any type in one check(fingers crossed) rather than checking every single type every single time.

equivalent JScript code(hypothetical, since JScript has typeof, and lacks many vb types):

var TypeDictionary = {
    0: 'vbEmpty',
    1: 'vbNull',
    2: 'vbInteger',
    3: 'vbLong',
    4: 'vbSingle',
    5: 'vbDouble',
    6: 'vbCurrency',
    7: 'vbDate',
    8: 'vbString',
    9: 'vbObject',
    10: 'vbError',
    11: 'vbBoolean',
    12: 'vbVariant',
    13: 'vbDataObject',
    17: 'vbByte'
};

var GetType = function() {
    return TypeDictionary[arguments[0]];
};
Dmitry
  • 4,438
  • 4
  • 30
  • 45