问题
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(recCount,2)
I get an error because recCount is of wrong type. I have tried to convert it to Int but that does not work either. The following works fine:
RsProxyList.Open objDBCommand,,1,1
dim recCount:recCount = RsProxyList.RecordCount
Dim output(3,2)
How do I convert recCount to get this array declaration to work?
回答1:
You need to first declare your Array as dynamic then use ReDim to set the first dimension dynamically, like this;
Dim output() 'Declare a "Dynamic Array"
ReDim output(recCount, 2) 'Set dynamic first dimension.
By specifying the dimensions like this;
Dim output(10, 2) 'Declare a "Fixed Array"
you tell VBScript to build a fixed array which does not accept variables as dimension declarations.
Dim output() versus Dim output
There seems to be some argument as to using
Dim output()
versus
Dim output
@Bond in the comments below came up with the most convincing one in my opinion for using
Dim output().Because VBScript stores all variables as
Variantdata type declaring anArrayusingDim outputis just like declaring any other variable, whereasDim output()creates anArray(0 sized array but an array nonetheless), this means there is one significant difference as far as the VBScript Runtime is concerned,Dim output()is anArray.What does this matter?, well it matters because functions like
IsArray()will still detect the variable as anArraywhere as "arrays" declared usingDim outputwill returnFalse.
Useful Links
@DougGlancy's answer to Redim without Dim? (vba but still relevant).
VBScript Reference - Dim Statement
VBScript Reference - VBScript Variables (specifically Array Variables sub heading).
来源:https://stackoverflow.com/questions/26059518/declaring-a-dynamic-array-not-working-as-expected