Dynamic, two dimensional array problem

# Thread: Dynamic, two dimensional array problem

1. Junior Member
Join Date
Dec 1969
Posts
17

## Dynamic, two dimensional array problem

Hi all,<BR><BR>I am trying to create a two dimensional array that has a dynamic number of "rows". I have done this before but I can&#039;t seem to get it to work now, so it&#039;s really driving me nuts. (And I can&#039;t find the other code anywhere :)<BR><BR>Here is an example of my code. In my attempts to figure out why it won&#039;t work I have simplified it as much as possible. Obviously I&#039;ll want to do this in some kind of loop when I get it working.<BR><BR>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim aTest<BR>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReDim aTest(0,1)<BR>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aTest (0,0) = "Name1"<BR>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aTest(0, 1) = "Value1"<BR>5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>6&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;ReDim Preserve aTest(1,1)<BR>7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aTest (1,0) = "Name2"<BR>8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aTest(1, 1) = "Value2"<BR>9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>10& nbsp;&nbsp;&nbsp;ReDim Preserve aTest(2,1)<BR>11&nbsp;&nbsp;&nbsp;aTest(2,0) = "Name3"<BR>12&nbsp;&nbsp;&nbsp;aTest(2,1) = "Value3"<BR><BR>When I run this code I get an error (Subscript out of range) at line 6. Does anybody know what I&#039;m doing wrong?

2. Senior Member
Join Date
Dec 1969
Posts
463

## RE: Dynamic, two dimensional array problem

Hi, I am actually dealing with the same problem. However, try declaring the array variable as dynamic. Only the first line changes<BR><BR>1 Dim aTest()<BR>2 ReDim aTest(0,1)<BR>3 aTest(0,0) = "Name1"<BR>4 aTest(0,1) = "Value1"<BR>5 <BR>6 ReDim Preserve aTest(1,1)<BR>7 aTest(1,0) = "Name2"<BR>8 aTest(1,1) = "Value2"<BR>9 <BR>10 ReDim Preserve aTest(2,1)<BR>11 aTest(2,0) = "Name3"<BR>12 aTest(2,1) = "Value3"<BR>

3. Senior Member
Join Date
Dec 1969
Posts
463

## NO, doesn't appear to work<EOM>

.

4. Senior Member
Join Date
Dec 1969
Posts
463

## Think I understand

like it is explained in this artcle <BR><BR>http://msdn.microsoft.com/library/en-us/script56/html/vsstmRedim.asp?frame=true <BR><BR>it is not possible to perform a "ReDim Preserve" on dimensions other than the last one. I learn this very unhappily.

5. Junior Member
Join Date
Dec 1969
Posts
17

## RE: Think I understand

Man that is most unfortunate. Well I guess we could just invert the array and give it a variable number of "columns" versus an variable number of "rows". Kinda opposite of a database. Makes it a little confusing, but it might work. Thanks for finding that answer rick!

6. Senior Member
Join Date
Dec 1969
Posts
463

## RE: Think I understand

doesn&#039;t matter, I was searching fro that answer too. I guess the method you said should work.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I don't understand the difficulty...

Have you ever used ADODB.RecordSet.GetRows?<BR><BR>If you had, you&#039;ll understand how/why the Redim Preseve is supposed to work.<BR><BR>With GetRows--and with your own code, with a tiny change, if you want to--the first dimension of the array is the *COLUMN* number. The *second* dimension is the row number.<BR><BR>That is:<BR><BR> contentsOfOneCell = arrayName( column_number, row_number )<BR><BR>And, so, to change the number of rows in your table, you use Redim Preserve. Presto. Exactly what you were after.<BR><BR>You might read these FAQs:<BR> http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=63<BR> http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=146<BR><BR>Incidentally, since the question *is* answered in the FAQs, this takes it out of the realm of "Advanced" per the guidelines of this forum. But if you want to debate about other possible methodologies, I&#039;d be glad to continue here.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Which is *exactly*...

...what ADODB.RecordSet.GetRows does. <BR><BR>But why do you think of it as "inverting"??? The choice of which dimension represents the column and which represents the row is, after all, arbitrary.<BR><BR>I will grant you that it is somehow more "human natural" to do row first, but it&#039;s a sure thing that the computer doesn&#039;t care...and neither should we, really.<BR><BR>

9. Junior Member
Join Date
Dec 1969
Posts
17

## RE: I don't understand the difficulty...

I havn&#039;t ever used GetRows. I&#039;ve seen it in the docs but never had a need for it.<BR><BR>As far as the first dimension being columns, I guess I was confused by Microsoft&#039;s documentation:<BR>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbsvariables.asp<BR><BR>If you look at the section about multidimensional arrays, they imply that the first dimension is the number of rows.<BR><BR>I appologize for posting this here if it does not qualify as advanced. I read the second article you suggested, but I guess at the time it didn&#039;t seem relevant to my question (I see now that it is). I didn&#039;t even find the first article you listed. I probably skipped over it because the title doesn&#039;t imply that there is any thing relating to multidimensional arrays in it.<BR><BR>In any case, thanks a bunch for making things clearer.<BR><BR>Rob

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## Boy, are you right!

Mea culpa. When you are mislead by the VBScript docs, no wonder you would get confused!<BR><BR>I withdraw my comment about "not advanced". Let&#039;s face it, when the docs are conflicting, this probably is the place you want to come.<BR><BR>Since VBS is unlikely to ever be upgraded, I doubt we can get the docs changed in the next version. Ah, well... Maybe we need to add another FAQ, to clarify this as much as possible.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•