Arrays

Milan

Registered User.
Local time
Today, 20:23
Joined
Feb 17, 2002
Messages
85
I need to store mutiple values into a variable, but i have never used an array. Can somebody give me an explanation on how to use an array. May a brief example would be very useful.

Thanks

Milan
 
Think of an array as multiple copies of a variable.
Typically they start 0 instead of 1, but depends on how and what you are using, for this I will start at 0 to make it more confusing.
NOTE: Syntax may not be 100% for the example

DIM MyArray (10) as string
Now you have basically 10 strings all called MyArray and each one has it's own address called a index or subscript or proaba;y many other names as well, but Access calls it an index. In this case 0 - 9
So MyArray(0) is the first one, MyArray(1) is the second one, etc.

If you picture an Excel spread sheet, that is nothing but a big 2 dimensional array they call cells. If you bring a the work sheets in to play, it is now 3 dimensional. So a 2D array would be:
DIM MyArray(10,10) as string
Now you have 100 strings called MyArray (10 times 10)
The first is MyArray(0,0), the second is MyArray(0,1) the 11th is MyArray(1,0) and the last one is MyArray(9,9) (remember base zero here).
Hope that is as confusing as mud.
 
That is much clearer, thanks for that!!
 
FoFa said:
Dim MyArray (10) As String
Now you have basically 10 strings all called MyArray and each one has it's own address called a index

I don't use arrays very often but wouldn't that just create one index in the array, basically the 11th index.

Where:

Code:
Dim MyArray(1 To 10) As String

would actually create an array of 10 indexed variables.


Also, Milan, just to add:

If you don't know how many pieces of data you are going to put into an array you can dimension it thusly.

Dim MyArray() As String

Note the lack of indexing within the array.


Now, when we want to add a variable to one of this array's indexes, we use the ReDim statement.

i.e.

Code:
Dim MyArray() As String
ReDim MyArray(0)
MyArray(0) = "Hello"

The only problem here is that if we continue to redimension our array when we assign data to it, all of the previous data throughout the array will be lost.

So, taking the code example above and adding a few lines to it:

Code:
Dim MyArray() As String
ReDim MyArray(0)
MyArray(0) = "Hello"[code]
ReDim MyArray(1)
MyArray(1) = "World!"

MsgBox MyArray(0) & " " & MyArray(1)

This code would display a message box with the message " World!" and not "Hello World!" as you may imagine as you have redimensioned the array variable meaning that all its contents are lost.

To get around this problem, we can introduce the Preserve keyword which allows us to retain information throughout the array while we alter one index.

i.e. putting all info in a listbox into an array

Code:
Dim MyArray() As String, intCounter As Integer
For intCounter = 0 To Me.lstListbox.ListCount - 1
   ReDim Preserve MyArray(intCounter)
   MyArray(intCounter) = Me.lstListbox.ItemData(intCounter)
Next intCounter
 
I don't use arrays very often but wouldn't that just create one index in the array, basically the 11th index.
Then would this be zero dimensions ReDim MyArray(0)
Is this one dimension then ReDim MyArray(1), or would this be just the second dimension? And how can you have just the 11th dimension without 1 through 10? Would it not be the first then?
Enquiring minds want to know?

Actually it can vary per language used, and usually it is the variable named followed by the number of occurances (and some allow the 1 to nn for clarity, but the result is the same). And some throw that base zero to make it harder! I find today I very infrequently use Arrays as opposed to the good old Mainframe days when it was standard coding for things.
 
Just to throw something else into the pot, you can override the 0 base by using Option Base 1 at the head of the procedure.
 

Users who are viewing this thread

Back
Top Bottom