|
-
Oct 13th, 2005, 07:14 PM
#1
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:
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.
Last edited by si_the_geek; Oct 14th, 2005 at 07:03 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|