Arrays in VB-1
Arrays in VB-1
Introduction
• An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same
type.
• For example, an array may consist of the number of students in each
grade in a grammar school; each element of the array is the number
of students in a single grade.
• Similarly, an array may consist of a student's grades for a class; each
element of the array is a single grade.
• It is possible to use individual variables to store each of these data
items. However, you would end up with so many variables
Cont.
• For example, if our application analyzes student grades, we can use a
separate variable for each student's grade, such as englishGrade1,
englishGrade2, etc.
• This approach has three major limitations:
• We have to know at design time exactly how many grades we have to handle.
• Handling large numbers of grades quickly becomes unwieldy. This in turn makes an
application much more likely to have serious bugs.
• It is difficult to maintain. Each new grade that we add requires that the application be
modified, recompiled, and redeployed.
Types of Arrays in VB.Net
• VB.Net has two main types of arrays namely:
• One-dimensional array-
• Multi-dimensional array
One dimensional Array
• As far as an array is concerned, one dimension means it has only one
value per location or index.
Multi Dimensional Array
• A multi-dimensional is an array that contains other arrays.
Cont.
• By using an array, you can refer to these related values by the same
name, and use a number that’s called an index or subscript to
identify an individual element based on its position in the array.
• When you use Visual Basic syntax to define the size of an array, you
specify its highest index, not the total number of elements in the
array.
• See next page for an example
• You can work with the array as a unit, and the ability to iterate its
elements frees you from needing to know exactly how many
elements it contains at design time.
Declaring One-dimensional array in VB
• The following line declares an array known as marks which stores marks
of 6 subjects.
• Dim marks() As Integer = {58, 68, 95, 50, 23, 89}
• Dim is used to show the scope of declaration while marks () is the array
name
• The parenthesis just after marks shows that this is an array and not an
ordinally variable.
• The curry braces are used to house the values/elements of the array
• The position of each element in the array is determined by an index.
Indexing starts from 0.
• The array can be said to be of size 5 since it has five indexes in it.
Indexing Continued
• The indexing of an array can be viewed as follows
• Dim marks() As Integer= {40, 55, 63, 17, 22, 68, 89, 97, 89}
• In this case we have an array of size 8 which means it has nine
elements the position of each element is shown below.
Cont.
• For example in the array known as marks the indexing is as follows.
• 58- index 0
• 68- index 1
• 95- index 2
• 50- index 3
• 23- index 4
• 89- index 5
Practice
• Write a visual basic program to declare an array with six values and
print the values
• See solution in Next page
Creating an array and outputting the first value
of the array
Module Module1 • This program creates an array
Sub Main() known as marks which has six
elements.
Dim marks() As Integer = {58, 68,
95, 50, 23, 89} • The program then uses indexing
to print/output the first element
Console.Writeline(marks(0)) in the array.
Console.Readkey()
End Sub
End Module
Creating an array and displaying the first
element in G.U.I
• The following is the simple G.U.I Public Class Form1
design. Private Sub Button1_Click(sender As
Object, e As EventArgs) Handles
Button1.Click
Dim marks() As Integer = {58, 68, 95, 50,
23, 89}
MessageBox.Show(marks(0))
End Sub
• End Class
Printing all elements of an Arrays
• Two main approaches can be used
1. Print each index value
2. Use a loop to iterate through the array and printout all the values of the
array
• Method 1 is easier but not suitable for huge arrays which have very
many elements. This is because it requires many lines of code which
consequently increases the complexity of the program
• The second approach is desirable for huge arrays but it may not be
suitable when you want to print non-continues indices.
• E.g say you have an array of size 8 and you want to print index 3, 5, and 8 a
loop may not work well in this case.
Printing all the elements of an array: Approach
1
Module Module1 Console.Writeline(marks(5))
Sub Main() Console.Readkey()
Dim marks() As Integer = {58, 68, End Sub
95, 50, 23, 89} End Module
Console.Writeline(marks(0))
Console.Writeline(marks(1))
Console.Writeline(marks(2))
Console.Writeline(marks(3))
Console.Writeline(marks(4))
Approach 2: Using a Loop to print all elements
of an Array
Module Module1 • This code uses a For..Each loop to
Sub Main() print the values of the array.
Dim marks() As Integer = {58, 53, 68, • It outputs all the values in the
95, 50, 23, 89} array.
Dim x As Integer • What if you want to output maybe
from the value in index 3 up to the
For Each x In marks last element?
Console.WriteLine(x) • In that case you would be forced to
Next use a For…Next loop.
Console.ReadKey()
End Sub
End Module
Approach 2: Using a Loop to print all elements
of an Array
Module Module1 • The code utilizes a For…Next loop
Sub Main() to perform the task of printing out
the values of the array from a
Dim marks() As Integer = {58, 53, defined starting point to a defined
68, 95, 50, 23, 89} ending point.
For i = 3 To marks.Length - 1 • In this case the starting point is 3
Console.WriteLine(marks(i)) which means the output will start
with the value in index 3.
Next
• For…Next loop is very ideal when
Console.ReadKey() you desire to define ranges
End Sub
End Module
Cont.
• Think about a situation in which a user would be required to input the
elements/ values of the array from keyboard and G.U.I then those
values are indexed in the array then with just a click of a button the
values are outputted/ displayed the screen.
• The steps in addressing this challenge:
• Create a windows form application
• Design the fields to capture the array values in this case we will just use three
• Add two buttons one for adding the items to the array ones its clicked the
other one is for displaying the array elements.
• Add a RichTextBox where the output will be displayed, you can also display it
on messageBox
This is how the design looks like
The coding part
• Here we need to declare the array outside any sub so that it can be
accessible to both buttons.
Public Class Form1
Dim marks(2) As Integer
• Next put this code below the add elements button
marks(0) = TextBox1.Text
marks(1) = TextBox2.Text
marks(2) = TextBox3.Text
• Put this code below the display button
RichTextBox1.Text = marks(0) & "," & marks(1) & "," & marks(2)
Full code: The colored code segments are
automatically generated
Public Class Form1 Private Sub Button2_Click(sender
Dim marks(2) As Integer As Object, e As EventArgs)
Handles Button2.Click
Private Sub
Button1_Click(sender As Object, e RichTextBox1.Text = marks(0) & ","
As EventArgs) Handles & marks(1) & "," & marks(2)
Button1.Click End Sub
marks(0) = TextBox1.Text End Class
marks(1) = TextBox2.Text
marks(2) = TextBox3.Text
End Sub
Multi-Dimensional Arrays
• A multi-dimensional array is an array that contains other arrays.
• The most commonly used multi-dimensional array is a two dimension
array
• A two dimension array can be represented in the form of a matrix (x,
y) where x represents the number of arrays and y represents the
number values in each array
• They are very useful especially when one is querying a database
because it makes it possible for one to represent multiple columns
and rows.
Cont.
• Think of an example in which you would like to represent the CAT
marks for three students as well as exam marks in a single array.
• The diagrammatic representation of this scenario
Cont.
• The situation can be presented in a table as follows
CAT EXAM
Student1 35 53
Student 2 33 37
Student 3 26 54
• The cat and Exam marks can be extracted to form a 2* 3 matrix
• Therefore this array can be said to be a 2*3 matrix
• If this array was to be presented in cartesian plane, the X-axis would have
CAT and Exam as its Values while Y-Axis would have Students as its Values
Declaration of the Multi-Dimensional Array
• The array can be declared as follows
• Dim marks(,) As Integer = {{35, 33, 26}, {53, 37, 54}}
• Note the , in the parenthesis to signify that this main array is
expected to have two-sub arrays in it.
• Note that all the two sub-arrays must have same number of elements
in them.
Indexing in a Two-Dimensional Array
• Indexing in a 2D array considers two factors:
• The index position of the sub-array in the main array
• The index position of the specific element in the sub-array
• Example: Dim marks(,) As Integer = {{35, 33, 26}, {53, 37, 54}}
• In this array called marks, the first sub-array has three elements {35,
33, 26}. This sub-array will be given index 0 while the second sub-
array will be given index 1.
• Each of these sub-arrays have three elements in them so in the first
sub-array 35- index 0, 33-index1, 26- index2.
• In the second-sub-array 53-index0, 37-index1, 54-index2.
Cont.
• How do you access element 53 in that array:
• Marks (1, 0)
• Note 1 represent index of sub-array location while 0 represents index of
element in the sub-array
• Code to print element 53
Dim anArray(,) As Integer = {{35, 33, 26}, {53, 37, 54}}
Console.WriteLine(anArray(1, 0))
Console.ReadKey()
Research Tasks
• Create a VB.Net windows form application that allows the user to
input the values of the 2D array from a G.U.I.
The End
Thank You