-
Nov 22nd, 2005, 09:26 PM
#1
Thread Starter
No place like 127.0.0.1
Classic VB - General - What are arrays and how do I use them?
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
Email(0) = "bob@example.com"
Email(1) = "steve@example.org"
Email(2) = "blah@example.net"
' ... and so on. up to
Email(8) = "jessica@example.com"
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
EmailA = "bob@example.com"
EmailB = "steve@example.org"
EmailC = "blah@example.net"
' ...
EmailI = "jessica@example.com"
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 currently holds: [email]bob@example.com[/email]
Output = Output & EmailB & " "
' Output currently holds: [email]bob@example.com[/email] [email]steve@example.org[/email]
' ...
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.
Last edited by eyeRmonkey; Nov 25th, 2005 at 01:36 PM.
-
Feb 6th, 2006, 05:27 PM
#2
Re: Classic VB - General - What are arrays and how do I use them?
Originally Posted by eyeRmonkey
VB Code:
For i = 0 To 8
sOutput = sOutput & Email(i) & " "
Next i
eyeRmonkey - Great FAQ but I just have one comment on the above. This assumes that the Option Base statement has been set to 0 and that there are always going to be 9 elements in the array. I think it is always safe to use the following syntax when looping through an array
VB Code:
For i = LBound(Email) To UBound(Email)
sOutput = sOutput & Email(i) & " "
Next i
Using this method you can be agnostic to both the Option Base statement and to the size of the array.
Last edited by si_the_geek; Feb 7th, 2006 at 10:01 AM.
-
Apr 20th, 2006, 02:04 PM
#3
Re: Classic VB - General - What are arrays and how do I use them?
One question that is often asked is "How do I sort an array?"
Here is a procedure that can be used to sort a multiple column array. As this procedure uses a basic bubble sort it should not be used for really large arrays.
VB Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Comments: This procedure sorts an array based on a column
' number, in either ascending or descending order.
'
' Arguments: OldArray The array to be sorted
' ColNum An optional column number for 2d
' arrays.
' ASC An optional boolean value to sort
' in ASCending or descending order.
'
' Date Developer Action
' --------------------------------------------------------------
' 09/04/05 Declan Kenny Initial version
'
Sub SortArray(ByRef OldArray As Variant, Optional ByVal ColNum As Integer = 1, Optional ByVal ASC As Boolean = True)
Dim Sorted As Boolean
Dim ArrayRec As Integer
Dim temp As Variant
Dim ColCount As Integer
'Loop until the Array is sorted
Do While Not Sorted
'Assume the array is in the correct order
Sorted = True
'Determine the sort order
If ASC Then
'Loop through the Array
For ArrayRec = 1 To UBound(OldArray, ColNum) - 1
'Are the current element and the next element in the wrong order
If OldArray(ArrayRec, ColNum) > OldArray(ArrayRec + 1, ColNum) Then
'If so, then swap the rows
For ColCount = 1 To UBound(OldArray, 2)
temp = OldArray(ArrayRec + 1, ColCount)
OldArray(ArrayRec + 1, ColCount) = OldArray(ArrayRec, ColCount)
OldArray(ArrayRec, ColCount) = temp
Next ColCount
'Record that we are not sorted
Sorted = False
End If
Next ArrayRec
'For Sorting in Descending Order
Else
'Loop through the Array
For ArrayRec = 2 To UBound(OldArray, ColNum)
'Are the current element and the next element in the wrong order
If OldArray(ArrayRec, ColNum) > OldArray(ArrayRec - 1, ColNum) Then
'If so, then swap the rows
For ColCount = 1 To UBound(OldArray, 2)
temp = OldArray(ArrayRec - 1, ColCount)
OldArray(ArrayRec - 1, ColCount) = OldArray(ArrayRec, ColCount)
OldArray(ArrayRec, ColCount) = temp
Next ColCount
'Record that we are not sorted
Sorted = False
End If
Next ArrayRec
End If
Loop
End Sub
Last edited by DKenny; Apr 21st, 2006 at 02:35 PM.
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
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
|