Publishing System Settings Logout Login Register
Working With Arrays And Bounds Checking
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on March 27th, 2007
4407 views
Visual Basic
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.

  • Command1
  • List1

2. make your way to the code...

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.

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"

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


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

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.

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.

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
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
sunjester

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top