在VBScript中获取变量的类型

时间:2010-07-19 13:19:04

标签: asp-classic vbscript

如何使用VBScript获取变量的类型?

6 个答案:

答案 0 :(得分:66)

VarType你需要什么?

  

返回表示变量子类型的值。

+--------------+-------+---------------------------------------------+
|   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                                       |
+--------------+-------+---------------------------------------------+
  

VarType函数永远不会返回Array的值。它   始终添加到某个其他值以指示a的数组   特殊类型。 Variant的值仅在具有时返回   已添加到Array的值以指示该参数   VarType函数是一个数组。例如,为。返回的值   整数数组计算为2 + 8192或8194.如果一个对象有   默认属性VarType(object)返回其默认类型   属性。

答案 1 :(得分:25)

如果要获取分配给Set变量的对象的类型名称,可以使用TypeName代替。

Class SomeClass
    '' empty class
End Class

Dim x
Set x = New SomeClass
WScript.Echo TypeName(x)  '' displays "SomeClass"

答案 2 :(得分:7)

Tmdean的答案也可以获取(几乎)所有其他类型变量的类型名称(不是整数)(每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()"

答案 3 :(得分: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

答案 4 :(得分:1)

vartype是合适的......

Dim x
x=123
msgbox VarType(x)

答案 5 :(得分: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

此版本投入了更多精力设置字典,但随后在一次检查(手指交叉)中查找任何类型,而不是每次都检查每种类型。

等效的JScript代码(假设,因为JScript具有typeof,并且缺少许多vb类型):

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]];
};