Discovering Arrays, Page 3
In this section, we're going to have our first fumbling frolic with arrays.
We'll be creating an array, putting information into it, then displaying certain data in message boxes. On the next page, we'll continue expanding this project with talk of loops and two important array keywords, LBound and UBound.
- Launch Visual Basic
First off, we're going to create our array and fill it with information.
- Click View, Code to enter the code window behind Form1
- Enter the following in the General Declarations section:
Dim MyArray(4) As String
This is your array. As you can see, there's not much difference between a regular string variable and a supercool array except the bracketed number. That number simply dictates how many items of information the array should hold.
- Add a Command Button to your Form
- Change it's Caption property to: Fill the Array
- Insert the following code behind the button:
Private Sub Command1_Click() ' Populate (fill) the array MyArray(0) = "Karl" MyArray(1) = "Ipy" MyArray(2) = "Katrina" MyArray(3) = "Mark" MyArray(4) = "Jill" End Sub
Do you understand what we've done so far? First off, we declared the array, passing it the number four. This tells the array it can hold up to five different pieces of information.
<Reader: Five? FIVE!?>
Yup, five. You see, in the wonderful world of arrays, counting is zero-based. That means you start at zero and work upwards. So if we declared a variable with the number three, it would have room for four pieces of information.
<Reader: Oh, I understand - you can have one if you want two. But can I have just one if I want to? And what if I want two but can't have one?>
<Ed: Hey, stop being difficult...>
<Karl: Mr Editor, please stop harassing my reader!>
<Ed: And you can zip it too, Karl. So where are ya working tomorrow?>
<Karl gets worried, puts on a serious face and suddenly attempts to change the subject>
Erm, yes, that code behind your Command Button erm, it simply inserts information into those array slots. In slot zero, we threw my name. Slot one saw my curiously christened friend, Ipy. Slot two, Katrina. And so on.
You wouldn't always throw information into arrays exactly like this however. Typically, you'd be retrieving values from a database or something equally as tech-savvy.
Well, that's all well and good, but at the moment you're simply taking my word that this array lark even works. Not that I shouldn't be trusted, but I must admit those thick eyebrows and crossed eyes do add a certain shadiness to my character.
But even if you run the project right now, unfortunately you'll not get any visible results all that array stuff is non-visual, it's like changing a variable in code. So let's add a little more code to test our hard work.
- Add another Command Button to your Form
- Change it's Caption property to: Get Stuff from Array
- Throw the following code behind the button:
Private Sub Command2_Click() On Error GoTo ArrayErr Dim intArrayNo As Integer intArrayNo = InputBox("Enter an array number to return:", _ "Choose a Number", 0) MsgBox MyArray(intArrayNo) Exit Sub ArrayErr: If Err.Number = 9 Then MsgBox ("No such item in the array!") Else MsgBox Err.Description End IfEnd Sub
It looks a little difficult, but this code simply grabs a number from the user and displays the corresponding item or 'element' from the array.
Well, that's the first bit of our application up-and-running. Go ahead and test it:
- Press F5 to run your application
- Try clicking on both your Command Buttons
Does it work? What happens when you enter a number outside our array range of zero to four? How about if you click our second command button before the first?