Simple VB Code and All About Arrays

Arrays are a very useful part of VB programming and can be used for many different things. Arrays are a collection of variables that are all the same type. They can be as big or little as you need them to be. For example, lets say I need to use 5 numbers in one of my applications. Instead of using 5 different integers and I can use an integer array. The following code is dimming the array for use in the application (As mentioned in the previous tutorial(s) any variable that you want to be used by the whole application needs to be dimmed right under the Public Class for the form.):

Public Class frmArrays
    Dim intNumber(4) As Integer
End Class

Notice how the intNumber is followed by a number that is enclosed in parenthesis. This number tells the application how large to make the integer. By this I mean we can store 5 different numbers in the integer we just created. (0 counts as one – so 4 actually means five.) To call then we simply put the number of the array we want to retrieve in those parentheses. Now that you know what an array is and what it does, let’s make an example and put it to use. Let’s make our form fist. As usual, I will show two forms, one with the names of the objects on the form, and one showing the normal text.


Note, you should make all of the text boxes in the lower portion ReadOnly. There is a ReadOnly setting in the properties. Set it to true.Now that we have our forms made, let’s being coding. As usual, let’s do our exit button first since it’s the easiest. Double-click on it and enter “End” as shown:

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    End Sub 

Our next section of code is the add integer button. We are going to make it add the integer to the next available slot in the array. In order to do this we are going to use another single integer to determine how many times we have added an integer to our array. So we need to Dim the integer times under our Class for the form (as above):

    Dim intTries As Integer

Now we need to add the code that will actually check for the array to be used 5 times. Since the integer that we created (intTries) will be starting at zero, we need to stop it once it reaches 5. We also need to make sure that the characters entered into the textbox that we have are indeed only numbers. To do this we will use a neat feature built into VB 2005 and greater called TryParse. It automatically tries to parse the data in the textbox and will fail if it contains text:

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        If intTries = 5 Then
            MsgBox("Array is full! Time to show values.", MsgBoxStyle.Exclamation, "Error!")
            If Integer.TryParse(txtInteger.Text, intNumber(intTries)) = False Then
                MsgBox("There are letters in the textbox, these cannot be added!", MsgBoxStyle.Exclamation, "Numbers Only!")
                intTries = intTries + 1
            End If
        End If
        txtInteger.Text = ""
    End Sub 

The first thing the code is doing is checking to make sure we have not reached the limit of our integer. It does this by checking our integer that we made. If it is it alerts the user that the array is full. If not it check the text box to see if there are any letters or punctuation in them. The code in that line is looking in the text box and sets the result to intNumber(intTries) – which in this case is intNumber(0). If there are no letters it will simply set it to whatever is in the textbox. If there are letters it will set it to zero. Our code only increments intTries up by one IF the textbox contains only number. The next time around it will be storing the integer to intNumber(intTries) again but this time it will be equal to intNumber(1). Lastly it blanks out our textbox for entry of another number. Now we need to set the code to show the array in our text boxes that we made on the bottom part of the form.

    Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
        txtInteger1.Text = intNumber(0)
        txtInteger2.Text = intNumber(1)
        txtInteger3.Text = intNumber(2)
        txtInteger4.Text = intNumber(3)
        txtInteger5.Text = intNumber(4)
        MsgBox("Integers need reset. Please click reset array to start over.", MsgBoxStyle.Exclamation, "Complete!")
    End Sub 

All this code does is set the text of the textbox equal to one of the integers in the array and alerts the user to reset the array before trying to add more integers to it. Now the last thing we need to do is add the code to the clear button.

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        intTries = 0
        intNumber(0) = 0
        intNumber(1) = 0
        intNumber(2) = 0
        intNumber(3) = 0
        intNumber(4) = 0
        txtInteger1.Text = ""
        txtInteger2.Text = ""
        txtInteger3.Text = ""
        txtInteger4.Text = ""
        txtInteger5.Text = ""
    End Sub 

This sets all integers back to zero and blanks out all of the text boxes on the bottom part of the form. This concludes the code we need to write. In the next tutorials I will show you how to prevent writing a 0 to each integer by using a Do, While statement to get it done easier. Now, let’s test our program. If you type in numbers and letters into the box and hit add to array you should see the following:


Now you should be able to add integers to it until you get the following box to alert you that it is full:


One the array is full we should now be able to show the contents of it in our boxes below. Click on Show Ints. to populate the boxes and you will get this reminder:


Once you click away you should see the integers that you typed in as shown below:


Now that you have seen them all you should be able to hit clear and start all over again. This concludes this tutorial and thank-you for reading.