Why does VarType() ALWAYS return 8204 for Arrays

前端 未结 3 1307
北恋
北恋 2021-01-12 00:54

In the VarType MSDN Microsoft documentation for VBScript\'s VarType function it says (With bold emphasis):

\"Remarks The VarType function

相关标签:
3条回答
  • 2021-01-12 01:35

    From documentation

    In VBScript, variables are always of one fundamental data type, Variant.

    Data contained in the variable can be of any type, but variables itself are always of Variant type. Checking with VarType the contents of a position of an array will return the type of data contained. But the array itself is a compound of "cells" of Variant type

    So, in your case VarType will return vbArray (8192) + vbVariant (12) = 8204

    0 讨论(0)
  • 2021-01-12 01:45

    Although VBscript lacks the syntax to declare arrays of any type other than "variant array of variants", its variant type and variant array type are not limited in that way. The variant type is a COM type, AKA a Visual Basic 4+ type, AKA a version of an Excel xloper (an Excel cell type).

    You can get VBscript "vartype" to return any other kind of array type by using it on an array created externally:

    obj = CreateObject("System.IO.MemoryStream")
    obj.SetLength 0
    obj.WriteByte 1
    obj.WriteByte 2
    
    A = obj.ToArray() 
    wscript.echo vartype(A)
    
    0 讨论(0)
  • 2021-01-12 01:48

    It's simply an error in documentation:

    ==>type D:\VB_scripts\SO\30511987.vbs
    option explicit
    Dim ii, aA(3)
    aA(1)=5
    aA(2)="string"
    aA(3)=Now
    
    Wscript.Echo "array", VarType(aA), TypeName(aA)
    
    For ii=0 To UBound(aA)
      Wscript.Echo "aA(" & CStr(ii) & ")", VarType(aA(ii)), TypeName(aA(ii))
    Next
    
    ==>cscript D:\VB_scripts\SO\30511987.vbs
    array 8204 Variant()
    aA(0) 0 Empty
    aA(1) 2 Integer
    aA(2) 8 String
    aA(3) 7 Date
    
    ==>
    
    0 讨论(0)
提交回复
热议问题