Click to See Complete Forum and Search --> : Some basic questions about two-dimensional arrays..


Pelle
02-23-2005, 04:04 PM
I would like to know how many rows and columns are created in the following array. Is it 10 rows and 2 columns or is it 10 columns and 2 rows?

Dim Array(9,1)

I have searched for some info about it but not found any consensus. Some sources claim that the first number symbolizes the rows and the second number the columns and other sources claim the opposite. I am confused.
What is it really like? How many rows and columns?
Assume that I need an array with 2 columns and 5 rows then how should it be declared (4,1) or (1,4)?
Which is the first dimension and which dimension it the second one? Horizontally (first/second) vs vertically (first/second)?

How are the various elements called, for example how is the element on the first row in the 5th column called. By using (0,4) or (4,0)?

Assume that I want to store first and last names in two separate columns in an array and then expand the array by using Redim/preserve. The dimension I want to expand is the one with the rows because I view the array as a table with two columns. I know there are certain rules that apply when it comes to using Redim/preserve on multidimensional arrays. Only the last dimension can be changed etc. How could that be done in this case? I am mainly interested in the declaration (1,x) or (x,1), where 1 is the number of columns and x number of rows (that is increased).

I guess the questions are quite simple but I am not sure about them...

phpnovice
02-23-2005, 04:21 PM
The reason you get two different answers about such things is because of the difference between how you program it and how it is actually stored. However, you don't need to be concerned with how it is stored -- just in how you reference it. Thus, you just have to remember the "R"oman "C"atholic system of dimensional theory. Ever seen a "C"atholic "R"oman? No. So... "R"ows always preceed "C"olumns -- or, "R"ows are always followed by "C"olumns.

russell
02-23-2005, 04:30 PM
the first dimension is thought of as the number of rows, and the second dimension is thought of as the number of columns.

arrays are zero based, so the 3rd element is at index position 2.

so ar(0, 3) is referencing the 4th element in the 1st row.

try this example script to help clarify

Sub TestArray()
Dim ar(1, 2)
Dim i
Dim j

ar(0, 0) = "a"
ar(0, 1) = "b"
ar(0, 2) = "c"
ar(1, 0) = "A"
ar(1, 1) = "B"
ar(1, 2) = "C"


Dim str

For i = 0 To UBound(ar, 1)
For j = 0 To UBound(ar, 2)
str = str & ar(i, j) & ", "
Next
str = str & vbCrLf
Next

Response.Write str
End Sub

TestArray