Search Tools Links Login

Simple Fixed Array, Part 2


In the last tutorial (Simple Fixed Array, Part 1) 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 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}

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 lets 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 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 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 Element 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 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, yVal 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"
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
End Sub
   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

About this post

Posted: 2019-09-20
By: Graham
Viewed: 244 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.