Visual Basic .NET

Arrays and the Index Number

This lesson is part of an ongoing tutorial. This first part
is here:

This lesson is part of an ongoing tutorial. This first part
is here:

In the previous part, you added some code to a button
in order to test out arrays. When you clicked your button, you probably got
an error box popping up, telling you that the “Index was outside the
bounds of the array
.” This is the error message you may have received:

An Index error

Or this one, in Community 2017 and 2019:

Index out of range error, Visual Studio 2017

The Index the error message is talking about is the figure in parentheses
in your array. We set up this array

Dim MyNumbers(4) As Integer

And the highest Index number is therefore 4. But we tried to display
something at index number 5:

MyNumbers(5)

Visual Basic said “Wait a minute, the idiot hasn’t got a position number
5!” So it stopped the programme and gave you an error message. Delete this
line from your code:

MessageBox.Show(“Sixth Number is: ” & MyNumbers(5))

So the way to get at information held in an array is through its Index number
– “What’s at array position 0? What’s at array position 1?” A very
handy way to get at the information in your array is by accessing its Index
number in a For Loop.

So that you don’t have all those message boxes popping up, we can display the
results in a List Box.

Add a List Box to your form. Make it fairly wide, and just leave it on the
default Name of ListBox1. Then change your code to the following
(the new code is in Bold, blue text):

Dim MyNumbers(4) As Integer

Dim i As Integer

MyNumbers(0) = 10
MyNumbers(1) = 20
MyNumbers(2) = 30
MyNumbers(3) = 40
MyNumbers(4) = 50

For i = 0 To 4

ListBox1.Items.Add(MyNumbers(i))

Next i

Run your programme, and click your button. Your form should look something
like this one:

The Listbox with the numbers displayed

The first time round the loop, the variable called i will hold the number
0. Visual Basic will test to see if our end condition is met. The end
condition was “Loop until the variable i holds the number 4“.
The variable i only holds the number 0, so Visual Basic drops
down to the next line:

ListBox1.Items.Add(MyNumbers(i))

And what is inside the variable i? The number 0. So what’s really getting
adding to the List Box is this:

MyNumbers(0)

In other words, “Add to the List Box whatever is inside the array at
position number 0

The next time round the loop, the variable i will hold the number 1. So this
gets executed

ListBox1.Items.Add(MyNumbers(1))

And the loop continues round and around, adding whatever is inside our array
until the end condition for the loop is met.

Change the first line of the For loop to this:

For i = 0 To 5

Can you guess what will happen? Try it an see. Make sure you know why you get
the error message before moving on.

Arrays and Strings of Text

Arrays can hold other types of data, too. They can hold Strings of text.

  • Put another Button on your Form
  • Set the Text property to “String Array”
  • Put the following code behind your Button

Dim MyText(4) As String

Dim i As Integer

MyText(0) = “This”
MyText(1) = “is”
MyText(2) = “a”
MyText(3) = “String”
MyText(4) = “Array”

For i = 0 To 4

ListBox1.Items.Add(MyText(i))

Next i

When you have finished, run the programme and click your new button. The text
you put into the 5 array positions should display in the List Box.

Again, the same process is at work: Set up an array, and specify how many items
you want to hold in the array; assign your data to each position; go round a
loop and access whatever is in each position of the array.

In the next part, we’ll take a closer look at assigning values to an Array.

Kaynak : https://www.homeandlearn.co.uk/NET/nets6p2.html ‘sitesinden alıntı

Yorum Yap