Working With Arrays And Bounds Checking
Views: 3095
Comments: 0
Posted: 3-27-2007
based on 2 votes.
Faves: 1 | + Faves
Page(s): 1
prerequisites: For loop, general controls

arrays are like lists, like a grocery list. arrays are an easy way to hold lots of data and load it. Before we start i want to cover soem code we will be using so you dont get scared or discouraged from the code. Below are some functions i will be using today, they all deal with arrays. I will not be covering multi-dimensional arrays in this tutorial.


1. load a new standard exe project. we are going to add some controls to the form, a command button and a listbox. It does not matter where you place these controls, just get them on the form. the names of the controls are listed below.


2. make your way to the code...

1
2
3
Private Sub Form_Load()
 
End Sub

we are going to load some arrays into the listbox using a for loop. First let's see how to use an array in code.

1
2
names = Array("george", "john", "paul", "another", "name")
List1.AddItem names(2)

this code will add paul to the listbox, list1. When arrays are use the way i have used it there, each data in the array is given an id starting with 0. so george is 0, john = 1, paul = 2... get it? good. We want to load all the values without having to do a .additem for each item in the array.  we will acheive this by using the FOR loop.

3. we can find the "upper" and "lower" number of an array, as in how many peices of data its holding. we do this with ubound and lbound.  lbound and ubound are functions that are used with arrays to help you keep track of and order arrays. Hopefully you can tell which one is the upper and which one is the lower. You may know this process as "bounds checking"

1
2
lowest = LBound(names)
highest = UBound(names)

now that we have the numbers for our FOR loop we can set it up...

1
2
3
4
5
6
7
8
9
Private Sub Form_Load()
names = Array("george", "john", "paul", "another", "name")
lowest = LBound(names)
highest = UBound(names)
 
For i = lowest To highest
   
List1.AddItem names(i)
   
Next i
End Sub

4. now that we can use our array listing, without unnecessary list1.additems galore, let's do soem more array checking. this time we will check to see if an array is an actual array. we do this ith, you guess it, ISarray. lol how easy eh? IsArray() returns true is an array is an array, or False if its not.

1
MsgBox IsArray(names)

5. here is my final code. there are a couple more things about arrays but this should be more than enough to get you started. if you need more, there is always the MSDN webpage, and google.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Private Sub Command1_Click()
End
End Sub
 
Private Sub Form_Load()
names = Array("george", "john", "paul", "another", "name")
lowest = LBound(names)
highest = UBound(names)
 
MsgBox IsArray(names)
 
For i = lowest To highest
   
List1.AddItem names(i)
Next i
End Sub
Page(s): 1
More tutorials from this author:

» No tutorials by this user.

View All Tutorials
sunjester - n/a
This author is too busy writing tutorials instead of writing a personal profile!
Close