2
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?

user692942
  • 16,398
  • 7
  • 76
  • 175
tomjm
  • 328
  • 2
  • 17
  • _dim recCount:recCount_ ???? Certainly not in VB.NET – Steve Sep 26 '14 at 12:31
  • The you should try `Dim recCount = CInt(RsProxyList.RecordCount)` and be sure to not retrieve more thant 32767 records – Steve Sep 26 '14 at 12:40
  • 1
    @Steve You can't declare and assign in Classic ASP like that, plus the OP has already said they *"tried to convert it to **int** but that does not work either"*. – user692942 Sep 26 '14 at 12:43
  • `.RecordCount` is already an `integer` the issue is you can't dynamically set the dimensions of a fixed array, it all comes down to how the array is declared. – user692942 Sep 26 '14 at 12:46
  • 3
    What is the array for?, you do realise if you want to populate an array from a `ADODB.Recordset` you can just use the `.GetRows()` method which returns a two dimensional array of columns and rows? – user692942 Sep 26 '14 at 12:50

1 Answers1

2

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 Variant data type declaring an Array using Dim output is just like declaring any other variable, whereas Dim output() creates an Array (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 an Array.

What does this matter?, well it matters because functions like IsArray() will still detect the variable as an Array where as "arrays" declared using Dim output will return False.


Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175