如何使用VBScript获取变量的类型?
发布于 2010-07-20 06:10:52
如果希望使用Set
获取赋值给变量的对象的类型名称,则可以使用TypeName
。
Class SomeClass
'' empty class
End Class
Dim x
Set x = New SomeClass
WScript.Echo TypeName(x) '' displays "SomeClass"
发布于 2012-02-18 00:50:17
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()"
发布于 2011-10-31 17:04:30
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
https://stackoverflow.com/questions/3281355
复制相似问题