Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Initialize 2D array in single statement

1. ## Initialize 2D array in single statement

Hi,
We can initialize values in 1D array in a single statement.
Dim arr1D
arr1D= Array("A","B","C","D")

Dim arr2D(5,0)
arr2D(0,0)="A"
arr2D(1,0)="B"
arr2D(2,0)="C"
arr2D(3,0)="D"
arr2D(4,0)="E"
arr2D(5,0)="F"

Can we store 2D array values(above one) in single statement like one dimensional array instead of multiple statements?

Regards,
Srinivas.V

No

3. ## Re: Initialize 2D array in single statement

You can if you use arrays of arrays. Not what you are asking for, but might solve your problem?

A = Array(Array(1,2,3),Array(4,5,6))
b = A(1)(1)

4. ## Re: Initialize 2D array in single statement

Yes:
<font class="small">Code:</font><hr /><pre>
Option Explicit
'
dim i, j, s
'
dim arr : arr= array(array("A",1,2,3), array("B",4,5,6))

for i=0 To ubound(arr)
'
s="arr("&amp;i&amp;"): "

for j=0 To ubound(arr(i))
'
s=s&amp;arr(i)(j)
'
if j&lt;&gt;ubound(arr(i)) then
s=s&amp;", "
end if
Next
msgBox s
next
</pre><hr />
The MsgBox output is:

arr(0): A, 1, 2, 3
arr(1): B, 4, 5, 6

Actually I suppose this is a "yes, but...". Notice that you use different syntax to dereference this two dimensional array:

above: arr(i)(j)
typical: arr(i,j)

While I have used this type of static initializer, I use is sparingly because it makes my head hurt with dimensions greater than two. Anyway, another tool in your bag.

The above code runs under WSH and should run as well under QTP.

5. ## Re: Initialize 2D array in single statement

I said "no" becuase it is not that easy as single dimensional array. Also he is new member so did not want him to confuse.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.