Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Dimensional Array 2

Status
Not open for further replies.

nphani

Technical User
Feb 10, 2002
104
0
0
US
This question might seem too simple, but I am completely new to VBScript and am finding it difficult to declare a 2-D array.
I want to declare an array as:

int arr = {{1,2,3}, {4,5,6}, {7,8,9} .. and so on... }

I have given the above in "C". How would I do that in VBScript? And also, how would I browse through the whole array?

Phani
 
Option Base 0
Dim Arr

Arr = Array(Array(1,2,3),Array(4,5,6),Array(7,8,9),...)


The Array function returns an array of the elements specified. Each of these elements may itself be an array.

To reference the required element try:

Element = Arr(1)(2) ' should return '6'

Element = Arr(1, 2) ' may also work - I'm not sure. Try it out.


 
Two problems, one small, the other a little larger...

There is no
Code:
Option Base 0
in VBScript, but this is easy: just remove it, you don't need it.

The technique shown above does not yield a 2-dimensional array at all. Instead you end up with a variant that contains a 1D array of 1D arrays. As noted, this will often serve just as well as an actual 2D array in VBScript.

But just as JScript cannot pass its "arrays" (which really are not arrays) when calling components, such a "variant containing an array of arrays" cannot always be passed to components that require an array (aka a "VBArray" object) parameter.

So while this can do what you want in many instances, be cautious and understand you don't necessarily have what you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top