# Visual Basic > Visual Basic FAQs >  Classic VB - How do I use one dimensional arrays instead of multidimensional?

## Merri

The fact about arrays in VB6 is that one dimensional, zero based arrays are many times faster than multidimensional arrays (and even more when compared to one based arrays). Many have, however, trouble understanding both one dimensional arrays in multidimensional use and the usage of zero base to their advantage.


Bring the zero on!

The great thing about zero based arrays is, simply put, that you don't need to separately define a value to be zero. When a variable is defined, it starts out as a zero. Thus:


VB Code:
Dim A As Long
Do Until A = 10
    A = A + 1
Loop
The very interesting part about this code is that it runs ten times. Thus it makes it more readable in a way, in this case. However, people often use For ... Next loops, thus you have to use 0 To 9 range to make a code run ten times.

Now, this still isn't a perfect reason to use zero based arrays. When you get to X and Y stuff, you can really understand why zero based arrays are handier than one based.


The X and Y in a one dimensional array

The basics are to follow. Let us start up with a simple grid of, say, 10 x 10 pixels. That makes up 100 pixels in total, thus array would be dimensioned using *ReDim Array(99)* to have hundred items (unless Option Base 1 is used, but I repeat myself: don't use it as there is no real good reason for it).

Now, the first item of the first line is Array(0). Thus the first item of the second line is the amount of X items in the first line: Array(10). This begins to feel logical when you get used to zero based arrays. There are more good reasons to come.

So, to loop through all elements in the array and to keep track of both X and Y values for whatever reason, you could do the following:

VB Code:
For Y = 0 To 9
    For X = 0 To 9
        Array(Y * 10 + X) = Calculation
    Next X
Next Y

And to optimize the speed:

VB Code:
For Y = 0 To 9
    For X = 0 To 9
        Array(XY + X) = Calculation
    Next X
    XY = XY + X
Next Y


When it gets really handy
Now, what if you wanted to calculate the X and Y positions of a certain array element? I've seen some horrible and complex codes and I did some myself when I were younger and didn't understand the brilliance and ease of zero based values.

Simply put: we have a backslash and Mod to help us in the task.


VB Code:
X = ArrayPosition Mod NumberOfHorizontalItems
Y = ArrayPosition \ NumberOfHorizontalItems

Very simple and very fast. No need to do any extra coding, thus very clean as well.

Mod returns the amount of items left after removing as much of the number it is given. For example:

11 Mod 10 = 1
21 Mod 10 = 1
546 Mod 10 = 6

Backslash does kind of the inverse: it tells how many times the compared number fits in the given number:

11 \ 10 = 1
21 \ 10 = 2
546 \10 = 54

This makes the use of zero based arrays a bliss and gives a good reasoning for it all. At times the speed gain can be remarkable.


Comment and feedback
You're welcome to send private messages as posting "thanks!" and the like is not allowed in the FAQ forum. You can post additional information of course in case I forgot to mention something. Zero based numbers are used commonly in programming so it is a good thing to learn the logic behind it and push back the "real life" way of thinking.

----------


## wossname

In addition to this I'd like to add the following:

Using Zero based arrays (in any languge) is advantageous when using random numbers to select items from an array.


VB Code:
Dim MyRandomIndex as integer
Dim MyArray(0 to 77) as string '78 elements in this array
...
Call Randomize
...
'select an index at random
MyRandomIndex = 78 * rnd
 msgbox(MyArray(MyRandomIndex))

This is really handy because you don't need to perform any array bounds checking.  This is because *rnd*  always returns a value:  (0 <= value < 1)

This means that MyRandomIndex will never try to select a string that is outside the array, lets look at the lowest and highest values that rnd can output...

Lowest: 0, so therefore (0 * 78) gives us an index of zero.  That is the first element in the array.

Highest: 0.9999999, which would be (0.9999999 * 78) giving us an index of 77 because the answer is rounded to an integer.

Because our array is bounded 0 to 77 this method is always fast and safe for selecting array elements randomly.



As Merri implies, 1 based arrays are evil and are only convenient to a human's brain. they just cause extra work for both programmer and CPU alike.  Your apps will run faster, use RAM more efficiently and be less bug prone if you ditch 1 based arrays (or even _n_ based arrays, which are even worse) and go for the pure, clean zero-based method.  :Smilie:

----------

