Simple Fixed Array: Part 2





2.00/5 (4 votes)
This is part two of of the Fixed Array aeries.
Introduction
In the last tutorial, we had a fixed array of elements (animals), and when we selected one of the radio buttons, a label would show us which number the element was. But, what if we had more than just six elements as in our array? Say, we had 500 elements and we needed to find out which element number the giraffe was? Well, that's where Array.IndexOf()
comes in to play. So, in part two of this tutorial, we will use Array.IndexOf()
. We will also place all the elements into a list box, and then we will use Array.Sort()
to rearrange the elements in alphabetical order. So let's start with the Dim
statements as follows:
Dim arrAnimal(6) As String
Dim AnimalIndex As Integer
Dim i As Integer
You will notice a change this time in the way we have dimensioned the array. Last time, we placed the elements like this: Dim arrAnimal() as String{"Lion","Tiger",Etc}
, and this time, we have not placed the elements in the array, we have just set a number inside the parentheses to let the array know that we intend to place 6 elements inside it at a later stage in the program. The next piece of code is when the Form load. This is the point in this program when the elements are placed inside the array.
Private Sub AnimalArray_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
arrAnimal(0) = "Lion"
arrAnimal(1) = "Tiger"
arrAnimal(2) = "Giraffe"
arrAnimal(3) = "Bear"
arrAnimal(4) = "Elephant"
arrAnimal(5) = "Monkey"
End Sub
Now, let's change the RadioButton1_CheckedChanged e
vent so that when it is checked, lblDisplay.Text
will show us which element number the animal is. You will also notice that we are using the concatenation character, &
, to join different parts of the text together. We have also used Chr(13)
. This is the Enter button on the keyboard, and tells the program to place the text that comes after it on to a new line. In the case of RadioButton1
, we want to know which element number Lion is: (Array.IndexOf(arrAnimal, "Lion")
.
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChange
lblDisplay.Text = "The Lion" & Chr(13) & "is Element [" &
(Array.IndexOf(arrAnimal, "Lion")) & "]" & Chr(13) & "in the Array"
End Sub
We do the same for the other five RadioButton
s in our program. The next piece of code places all of the elements in our array into a list box when we click Button1
, using a For Next Loop
; notice the numbers 0 to 5, this is because in an array, the first element index is 0.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnShowall.Click
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
Now that the elements are in the ListBox
, we can click on Button2
and get them sorted in alphabetical order using Array.Sort()
. Notice that before we sort the elements, we clear the contents of the ListBox
first using ListBox1.Items.Clear()
. After sorting the array, if you click on one of the radio buttons, you will see that the element number now matches the sorted array, so that the Bear, element number [3] unsorted, is now element number [1].
Private Sub btnSort_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSort.Click
ListBox1.Items.Clear()
Array.Sort(arrAnimal)
For i = 1 To 6
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
Here is the complete code for you to copy (Ctrl C) and paste (Ctrl V).
Public Class AnimalArray
Dim arrAnimal(6) As String
Dim AnimalIndex As Integer
Dim i As Integer
Private Sub AnimalArray_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
arrAnimal(0) = "Lion"
arrAnimal(1) = "Tiger"
arrAnimal(2) = "Giraffe"
arrAnimal(3) = "Bear"
arrAnimal(4) = "Elephant"
arrAnimal(5) = "Monkey"
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
LblDisplay.Text = "The Lion" & Chr(13) & "is Element [" & _
(Array.IndexOf(arrAnimal, "Lion")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
LblDisplay.Text = "The Tiger" & Chr(13) & "is Element [" & _
(Array.IndexOf(arrAnimal, "Tiger")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
LblDisplay.Text = "The Giraffe" & Chr(13) & "is Element [" & _
(Array.IndexOf(arrAnimal, "Giraffe")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
LblDisplay.Text = "The Bear" & Chr(13) & "is Element [" & _
(Array.IndexOf(arrAnimal, "Bear")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
LblDisplay.Text = "The Elephant" & Chr(13) & " is Element [" & _
(Array.IndexOf(arrAnimal, "Elephant")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
LblDisplay.Text = "The Monkey" & Chr(13) & " is Element [" & _
(Array.IndexOf(arrAnimal, "Monkey")) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnShowall.Click
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array"
Next
End Sub
Private Sub btnSort_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSort.Click
ListBox1.Items.Clear()
Array.Sort(arrAnimal)
For i = 1 To 6
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
End Class