Arrays are a useful feature of any programming language. They allow you to store a lot of data in a systematic format. When combined with loops, they become an extremely powerful tool.
Things you should know before hand:
* How to declare a variable
* How to join two strings together
* How to use loops
* How to type 
So lets get cracking...
The Basics
When you declare a variable the normal way, it would like this:
... this is just one variable and has 1 place in the memory of the computer. Now, suppose you want a lot of strings that will all be holding similar information. Maybe it is a list of email addresses. How would you store all these? You could do this:
VB Code:
Dim EmailA As String
Dim EmailB As String
Dim EmailC As String
' And so on...
But that is very sloppy and hard to work with because if you want to do some work with those email address you have to do it to each one and end up using copy-paste a lot. Now when you declare it like this:
... you are telling the computer that you want 9 strings put into memory. It is 9 because arrays start counting at 0 by default so you have 0 through 8. The number in parenthesis tells the computer how many you want to make. From here on out, where ever we put a number in parenthesis it will be referring to a specific item out of the 9 we have declared (also called an element). So if we did this:
VB Code:
Dim Email(8) As String
' Now we store strings into each of the elements in the array
' Email(0) just means we want to deal with the first element
' of the "Email" array
' ... and so on. up to
that would be the equivalent of doing this (without arrays):
VB Code:
Dim EmailA As String, Dim EmailB As String, Dim EmailC As String
Dim EmailD As String, Dim EmailE As String, Dim EmailF As String
Dim EmailG As String, Dim EmailH As String, Dim EmailI As String
' ...
Putting Arrays to Work
So far the only advantages of arrays that we have seen is that they are easier to declare because you don't have to write out each variable.
Now, say we wanted to string all of these together and print them to a text box. Here is how we would do it the bad way (without arrays):
VB Code:
' Assume we have already declared and stored values
' into the variables in the last code example
Dim Output As String
' String together the emails and add a space after each
Output = Output & EmailA & " "
Output = Output & EmailB & " "
' ...
Output = Output & EmailI & " "
' Output should hold all 9 email address with a space between them, but
' it took a lot of code to get there.
That is obviously very redundant coding and would get to be a pain in the butt very quickly. Luckily we have arrays available to use and we can do it like this:
VB Code:
' Assume we have already put value into the array as
' we did 3 code boxes ago.
Dim i As Long
Dim sOutput As String
' Loop 9 times
For i = 0 To 8
sOutput = sOutput & Email(i) & " "
Next i
That does the exact same thing as the last code box, but with much less code.
Clearing The Array
Now, if you want to erase an array before you continue using it there are two methods you can use. The first is fairly straight forward:
This simply sets every value in the back to an empty string. It would be the same as doing this (without arrays):
VB Code:
EmailA = ""
EmailB = ""
' ...
EmailC = ""
The other way to clear the contents of an array is to use the "ReDim" statement as so:
The only difference is that you put a number in the parenthesis. This number must be the length of your array just like when you originally declared it.
Erase is easier when you simply want to clear an array, but ReDim has a lot more power than simply clearing an array. We can't go over all of the functionality of ReDim here, but I hope to write a "Advanced Arrays" tutorial in the near future, so keep your eyes open.
With this new knowledge under your belt, you can go out into the world and tackle some major problems with some relatively simple code. If you have any questions on this subject feel free to post them in the Classic VB Forum.