MRidha AL-Kaabi Lectures of VB.
Net: Lecture 6
Declaring and Accessing Arrays
Arrays: create a group of variables with the same data type.
In an array
– Each element behaves like a variable.
– All elements must have the same data type.
– Elements either can contain primitive data or can be reference variables.
Arrays can be either one - dimensional or multidimensional.
One - dimensional array consists of elements arranged in a row.
Two - dimensional array has both rows and columns.
Three - dimensional array has rows, columns, and pages.
VB .NET implements multi-dimensional arrays as arrays of arrays.
Using One-Dimensional Arrays
Declare a 5-element array with of integers:
' declare an integer array with 5 elements
Dim A(5) As Integer
Individual array elements are accessed by writing the array reference
variable, followed by the index value of the element enclosed in
parentheses.
Code to initialize the array elements:
A(1) = 75
A(2) = 80
A(3) = 70
A(4) = 85
A(5) = 90
1
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
An array can be declared and populated using a single statement:
Dim A() As Integer = {75, 80, 70, 85, 90}
A five elements Integer Array
Example 1:
Write a program in VB. Net to read array X(10) then find the summation of
all elements in this array using Do … While statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I As Integer = 1
Dim X(10) As Integer
Dim Y As Integer = 0
Do While I <= 10
X(I) = InputBox("Enter The Value of Array X ")
Y = Y + X(I)
I=I+1
Loop
[Link]("The Summation is: " & Y)
End Sub
2
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Example 2:
Write a program in VB. Net to read array B(5) then find the largest element in this
array using For … Next statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I As Integer = 0
Dim B() As Integer = {5, 80, 100, 60, 30}
Dim Max As Integer = B(0)
For I = 1 To 4
If B(I) > Max Then
Max = B(I)
End If
Next
[Link]("The Largest Element of Array is " & Max)
End Sub
3
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Example 3:
Write a program in VB. Net to read array M(10) then multiply each element in
this array by (5) using For .. Next Statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I As Integer
Dim M(10) As Integer
Dim W(10) As Integer
For I = 1 To 10
M(I) = InputBox("Enter The Values of Array M ")
W(I) = M(I) * 5
Next
For I = 1 To 10
[Link](M(I) & " ")
Next
[Link]()
[Link]()
For I = 1 To 10
[Link](W(I) & " ")
Next
End Sub
4
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Example4:
Design the following form and write a program in VB. Net to read two arrays A(5),
B(5) then find the summation of elements between arrays A and B in new array
C(5) using For .. Next Statement?
The Solution is:
To design the above form we implement the following set properties of the objects:
Object Property Setting
Button1 Text Sum
Button2 Text Clear
Button3 Text Exit
Label1 Text Array A
Labele2 Text Array B
Labele3 Text Array C
Form1 Text Summation of Two Arrays
Form1 MaximizeBox False
Forrm1 MinimizeBox False
ListBox1 [Link]( ) Elements of A(5)
5
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
ListBox2 [Link]( ) Elements of B(5)
ListBox3 [Link]( ) Elements of C(5)
Writing the Code
Use the Code Editor
1. Double-click the Sum Button1 on the form and write the following code
inside it:
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles [Link]
Dim I As Integer
Dim A(5) As Integer
Dim B(5) As Integer
Dim C(5) As Integer
For I = 1 To 5
A(I) = InputBox("Enter The Elements of Array A")
[Link](A(I))
Next
For I = 1 To 5
B(I) = InputBox("Enter The Elements of Array B")
[Link](B(I))
Next
For I = 1 To 5
C(I) = A(I) + B(I)
[Link](C(I))
Next
End Sub
6
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
2. Double-click the Clear Button2 on the form and write the following code
inside it:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles [Link]
[Link]()
[Link]()
[Link]()
End Sub
3. Double-click the Exit Button3 on the form and write the following code
inside it:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles [Link]
[Link]()
End Sub
7
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Using Multidimensional Arrays
Conceptually
A two - dimensional array is like a table with rows and columns.
A three - dimensional array is like a cube, with rows, columns, and pages.
Each dimension has its own index.
Declare an Integer array with five rows and two columns.
Example:
Dim A(3, 3) As Integer
Example 1:
Write a program in VB. Net to read array A(3, 3) then print this array using
For .. Next Statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I, J As Integer
Dim A(3, 3) As Integer
For I = 1 To 3
For J = 1 To 3
A(I, J) = InputBox("Enter The Values of Array A ")
Next
Next
For I = 1 To 3
For J = 1 To 3
[Link](A(I, J) & " ")
Next
8
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
[Link]()
Next
End Sub
Example 2:
Write a program in VB. Net to read array B(3, 3) then find the largest element in
this array using For … Next statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I, J As Integer
Dim B(3, 3) As Integer
Dim Max As Integer = B(1, 1)
For I = 1 To 3
For J = 1 To 3
B(I, J) = InputBox("Enter The Values of Array A ")
Next
Next
For I = 1 To 3
For J = 1 To 3
If B(I, J) > Max Then
Max = B(I, J)
End If
Next
Next
[Link]("The Largest Element is " & Max)
End Sub
9
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Example 3:
Write a program in VB. Net to read array B(3, 3) then multiply each element in
this array by (5) using For .. Next Statement?
Writing the Code
Use the Code Editor
Double-click the Button1 on the form and write the following code inside it:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles [Link]
Dim I, J As Integer
Dim B(3, 3) As Integer
Dim C(3, 3) As Integer
For I = 1 To 3
For J = 1 To 3
B(I, J) = InputBox("Enter The Values of Array A ")
C(I, J) = B(I, J) * 5
Next
Next
For I = 1 To 3
For J = 1 To 3
[Link](B(I, J) & " ")
Next
[Link]()
Next
[Link]()
For I = 1 To 3
For J = 1 To 3
[Link](C(I, J) & " ")
10
MRidha AL-Kaabi Lectures of [Link]: Lecture 6
Next
[Link]()
Next
End Sub
Homework 1:
Write a program in VB. Net to read two arrays A(3, 3), B(3,3) then find the
summation of elements between arrays A and B in new array C(3,3)
using For .. Next Statement?
Homework 2:
Write a program in VB. Net to read two arrays A(3, 3), B(3,3) then find the
Multiplication of elements between arrays A and B in new array C(3,3)
using For .. Next Statement?
11