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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multi dimensional arrays ques 2

Status
Not open for further replies.

johngrg

Programmer
Apr 19, 2007
17
US
Environment: VB6

1. to declare multi dimensional array, can I use either one of the following?

Dim arr(2)(5) or Dim arr(2,5)?

2.
Private Sub GetData(arr as Variant)
arr = Array(arr(1)(1), arr(5)(1))
End Sub

Is it retrieving the value from arr array (array 1 element 1, array 5 element 1) and creating another array again?


Thankyou.
 
<Dim arr(2)(5) or Dim arr(2,5)

If you do the first one, you will find that you get a syntax error.

<Is it retrieving the value from arr array (array 1 element 1, array 5 element 1) and creating another array again?

No, it's throwing a syntax error.

HTH

Bob
 
Try Dim arrYourArray(1,3,5) As Variant (using whatever dimensions)

Did you hear about the Buddhist who refused Novocain during a root
canal? He wanted to transcend dental medication.
 
>No, it's throwing a syntax error.

Technically, it will not throw a syntax error. However, it may generate a runtime error.
 
I stand corrected. A compile error is what it throws. I assumed it would turn all red just like the previous line.
 
Actually, there are circumstances where you might use the form arr(x)(y).

If you are using multiple 2d arrays and will frequently want to transfer whole columns (or rows) between the arrays, then you can do so without looping if, instead of using a single 2d array, you store your data as 1d arrays of variants, each of which is itself a 1d array.

Even so, you still would not dimension it as Dim arr(3)(5).

So, for example:
Code:
Dim a(1 To 5) As Variant
Dim b(1 To 4) As Integer
Dim c(1 To 8) As Variant
Dim x As Integer, y As Integer
Dim q As Integer

'make an EFFECTIVELY 2d array holding the
'numbers 1 to 20 in 5 rows of 4 items
For x = 1 To 5
  For y = 1 To 4
    b(y) = (4 * (x - 1)) + y
  Next y
  a(x) = b()
Next x

'to show that you can individually access the items in 
'array a() by row & column, get the 2nd item from 
'the 4th row of array(a)
q = a(4)(2)

'to show that you can transfer whole rows without looping 
'from one array to another, make the 6th row 
'of the array c() equal to the 3rd row of the array a()
c(6) = a(3)

Hope that helps,
Tony
 
Bob, that was my point. Arr(1)(1) is syntactically correct in VB as long as it is used as an expression. However, it cannot be used in Dim statement (or any other variable declaration statement) as mentioned by yourself and Tony.

You can put the above mentioned GetData function without any syntax or compile error in your code. You can even produce an EXE, the code will compile without any error.

For most values of the argument, the you will get a runtime error. But for some very specifically formatted values of argument, the code will run without any errors.

See the following for instance.
___
[tt]
Private Sub Form_Load()
Dim V As Variant
GetData 0 'will throw a runtime error

V = Array(, Array(, 1), , , , Array(, 2))
GetData V 'code will run without error
'at this stage, V is an array of 2 items; V(0)=1, V(1)=2
'can be verified from the Locals window
Stop
End Sub
Private Sub GetData(arr As Variant)
arr = Array(arr(1)(1), arr(5)(1))
End Sub[/tt]
___

Try running the above code. It will give a runtime error during the first function call.

Now comment out [tt]GetData 0[/tt] and run again, the second function call [tt]GetData V[/tt] will run without any error.
 
Interesting. I didn't know this stuff! Thanks for sharing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top