Results 1 to 2 of 2

Thread: Classic VB - How do I use one dimensional arrays instead of multidimensional?

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Classic VB - How do I use one dimensional arrays instead of multidimensional?

    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:
    1. Dim A As Long
    2. Do Until A = 10
    3.     A = A + 1
    4. 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:
    1. For Y = 0 To 9
    2.     For X = 0 To 9
    3.         Array(Y * 10 + X) = Calculation
    4.     Next X
    5. Next Y

    And to optimize the speed:
    VB Code:
    1. For Y = 0 To 9
    2.     For X = 0 To 9
    3.         Array(XY + X) = Calculation
    4.     Next X
    5.     XY = XY + X
    6. 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:
    1. X = ArrayPosition Mod NumberOfHorizontalItems
    2. 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.
    Last edited by si_the_geek; Oct 14th, 2005 at 07:03 PM.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Classic VB - How do I use one dimensional arrays instead of multidimensional?

    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:
    1. Dim MyRandomIndex as integer
    2. Dim MyArray(0 to 77) as string '78 elements in this array
    3. ...
    4. Call Randomize
    5. ...
    6. 'select an index at random
    7. MyRandomIndex = 78 * rnd
    8.  
    9. 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.
    Last edited by si_the_geek; Mar 1st, 2007 at 03:44 PM. Reason: (fixed code tags issues with new forum formatting)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width