Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,403 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,228 people online right now. Registration is fast and FREE... Join Now!




Simple Fixed Array Part1

 
Reply to this topicStart new topic

> Simple Fixed Array Part1, A tutorial for Beginners.

Graham
Group Icon



post 10 Jun, 2006 - 09:53 AM
Post #1


When I started programming the one thing that would trick me was Arrays.
So, with this in mind, I have decided to write 3 tutorials to try and help other beginners.
Parts 2 and 3 are here.
Simple Fixed Array Part2
Simple Fixed Array Part3
What follows is a fixed array, an array is simply a container for a list of elemants 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.
CODE

Public Class Form1
  
End Class

Change the Form1 to arrAnimal, see below
CODE

Public Class arrAnimal
  
End Class

Next we need to dimension the array using the Dim statement. Don't worry about the parentheses after the arrAnimal, we will use them next time, because the elements of this array are text I have used the As String type.

CODE

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
CODE
LblDisplay.Text = arrAnimal(0)
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).
CODE

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,eg 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 fixed Array.
Use in a program? could be used for a Hangman type game.
If this tutorial is of help and I get some feedback then I will expand the next tutorial to see what other things can be done with arrays.

This post has been edited by Graham: 29 Jun, 2006 - 11:59 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

GoldSquarz
*



post 6 Mar, 2008 - 12:21 PM
Post #2
Thank You very much for the example of coding in VB using arrays. I will practice this. I know that I can put this to good use some how. This is my first semester working with VB and I like it a lot. I think that I want to use this technique to develop some real easy to use graphic interfaced VB game application for my toddler. She seams very fasinated with my computer and what I am doing all the time. I think that I can spark her curiousity even more if I could get the bottons to make objects move; light up; and make sounds. She would love it, and I think that it would be easy enough for her to learn how to press the bottons down on one side of the keyboard to make something happen and another side for a different result. If you like my idea; please help yourself and utilize it for your own app. Thanks again for the insight concerning the 1st array tutorial.

Jason
Go to the top of the page
+Quote Post

sam.adams61
*



post 14 Jul, 2008 - 01:14 PM
Post #3
Super stuff!! Many thanks for your interest in posting such info for newbies like myself. Coming from an HGV drivers background, you'll appreciate how much I appreciate such valuable help in learning to program using vb.net. Keep up the good work. thumbs-up.gif I'll now look for Part2. biggrin.gif
Go to the top of the page
+Quote Post

gabehabe
Group Icon



post 14 Jul, 2008 - 01:17 PM
Post #4
There's already a part 2, and a part 3!

Part 2
Part 3
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/22/08 08:08AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month