Click here to Skip to main content
15,886,963 members
Articles / Programming Languages / Visual Basic
Article

Simple Fixed Array Part 1

Rate me:
Please Sign up or sign in to vote.
1.67/5 (8 votes)
10 Jul 20062 min read 21K   15   1
Shows how to create a Simple Fixed`Array

Sample Image - Simple_Fixed_Array_Part_1.gif

Introduction

When I started programming the one thing that would trick me was Arrays. So, with this in mind, I have decided to write some tutorials to try and help other beginners. What follows is a one dimensional array, an array is simply a container for a list of items in the case of this example it is a list of animals. First start a New Project, then in solutions explorer click on the form then click view code, you should then see the following.

VB
Public Class Form1 

End Class

Change the Form1 to arrAnimal, see below:

VB
Public Class arrAnimal 

End Class

Next we need to dimension the array using the Dim statement.

VB
Public Class arrAnimal

Dim arrAnimal() As String = _
      {"Lion", "Elephant", "Tiger", "Giraffe", "Bear", "Monkey"}

End Class

You will notice that I have already placed the elements (Animals) in the array ready to be called by the program in the order that they appear, so if for example I was to call array element 3 I would be given ‘Giraffe’, hands up all those who thought it was going to ‘Tiger’ well the reason the program returned ‘Giraffe’ is that the elements in the array are numbered 0,1,2,3,4,5 giving a total of six elements, Yes I know it’s weird but what can you do? The next thing we need to do is to add six radio buttons to our form and one label,you can change the name of the Label to LblDisplay. When you have done this double click on the first RadioButton to bring up the RadioButton1_CheckedChanged event, and copy this code in to it and do the same for all the other radio buttons so that your program now looks like this. Or you can just Copy (Ctrl+C) and Paste (Ctrl + V).

VB
Public Class AnimalArray
  Dim arrAnimal() As String =  _
      {"Lion", "Elephant", "Tiger", "Giraffe", "Bear", "Monkey"}

  Private Sub RadioButton1_CheckedChanged(ByVal sender _
          As System.Object, ByVal e As System.EventArgs) _
          Handles RadioButton1.CheckedChanged
    LblDisplay.Text = arrAnimal(0)
  End Sub

  Private Sub RadioButton2_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles RadioButton2.CheckedChanged
    LblDisplay.Text = arrAnimal(1)
  End Sub

  Private Sub RadioButton3_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles RadioButton3.CheckedChanged
    LblDisplay.Text = arrAnimal(2)
  End Sub

  Private Sub RadioButton4_CheckedChanged(ByVal sender As _
              System.Object, ByVal e As System.EventArgs) _
              Handles RadioButton4.CheckedChanged
    LblDisplay.Text = arrAnimal(3)
  End Sub

  Private Sub RadioButton5_CheckedChanged(ByVal sender _
          As System.Object, ByVal e As System.EventArgs) _
          Handles RadioButton5.CheckedChanged
    LblDisplay.Text = arrAnimal(4)
  End Sub

  Private Sub RadioButton6_CheckedChanged(ByVal sender _
          As System.Object, ByVal e As System.EventArgs) _
          Handles RadioButton6.CheckedChanged
    LblDisplay.Text = arrAnimal(5)
  End Sub
End Class

Now you can run the program and as you click each RadioButton the RadioButton_CheckedChanged event will place the animal from the array into the LblDisplay, e.g., clicking RadioButton 4 will display arrAnimal(3) which, if you have been paying attention, you will know is the Giraffe. And that,s it, your first one dimensional Array. Use in a program? could be used for a Hangman type game.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
JokeGet ready to tweak your remote config files with this article Pin
AbuseByUnkindPeople18-Jul-06 13:10
AbuseByUnkindPeople18-Jul-06 13:10 
Pure rocket science!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.