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 determine which element number the Giraffe was? Well that's where Array.IndexOf() is relevant.
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 into 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 at a later stage in the program.
The next piece of code is when the Form_Loads 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 Event so that when it is checked the lblDisplay.Text will show us which element number the animal is, you will also notice that we are using concatenation the & character to join 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 the 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 RadioButtons 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 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 into alphabetical order using Array.Sort() like this. Notice that before we sort the elements we clear the contents of the ListBox first using ListBox1.Items.Clear(). After you have sorted the array if you click on one of the RadioButtons you will see that the element number now matches the sorted srray, 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