0% found this document useful (0 votes)
42 views

Arrays: A Group of Items (String, Numbers, Dates, Image, Etc.) Arranged in Rows and Columns Is Called An

The document discusses arrays in Visual Basic. It explains that arrays allow a series of variables to be referred to by the same name using an index number. The document provides examples of declaring fixed-size and dynamic arrays, including assigning values to array elements and retrieving output. It also includes an example program that inputs marks for 8 subjects, calculates the total marks, and displays the contents of the array and total.

Uploaded by

njk19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Arrays: A Group of Items (String, Numbers, Dates, Image, Etc.) Arranged in Rows and Columns Is Called An

The document discusses arrays in Visual Basic. It explains that arrays allow a series of variables to be referred to by the same name using an index number. The document provides examples of declaring fixed-size and dynamic arrays, including assigning values to array elements and retrieving output. It also includes an example program that inputs marks for 8 subjects, calculates the total marks, and displays the contents of the array and total.

Uploaded by

njk19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 27

Arrays

A group of items (String,Numbers,Dates,image, etc.) arranged in rows and columns is called an Array.

IT Department - National Institute of Education

End Show

List of Students

Saman

Kamal

Anil

Ranil

Sunil

Student Array

IT Department - National Institute of Education

End Show

Arrays
Student(0)

Saman

Kamal

Anil

Ranil

Sunil

Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. Visual Basic allocates space for each index number and avoids declaring an array larger than necessary
End Show

IT Department - National Institute of Education

Arrays
Student(1)

Saman

Kamal

Anil

Ranil

Sunil

Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. Visual Basic allocates space for each index number and avoids declaring an array larger than necessary
End Show

IT Department - National Institute of Education

Arrays
Student(2)

Saman

Kamal

Anil

Ranil

Sunil

Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. Visual Basic allocates space for each index number and avoids declaring an array larger than necessary
End Show

IT Department - National Institute of Education

Arrays
Student(3)

Saman

Kamal

Anil

Ranil

Sunil

Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. Visual Basic allocates space for each index number and avoids declaring an array larger than necessary
End Show

IT Department - National Institute of Education

Arrays
Student(4)

Saman

Kamal

Anil

Ranil

Sunil

All the elements in an array have the same data type.

IT Department - National Institute of Education

End Show

Cont..

But when the data type is Variant, the

individual elements may contain


different kinds of data (objects,

strings, numbers, and so on).

IT Department - National Institute of Education

End Show

Cont..
There are two types of arrays in Visual Basic

1. Fixed-size array which always remains the same size,

2. Dynamic array:size can be changed at runtime

IT Department - National Institute of Education

End Show

Declaring Fix size arrays There are three ways to declare a fixed-size array; depending on the scope you want the array to have:

Public array,
Use the Public statement in the Declarations section of a module to declare the array.

Public StName(9) As String Public SubjectM(8) As integer

' 10 elements. ' 9 elements.

IT Department - National Institute of Education

End Show

Declaring Fix Cont..


Module-level array,

Use the Private statement in the Declarations


section of a module to declare the array. Private StName(9) As String '10 elements.

Private SubjectM(7) As integer ' 8 elements.

IT Department - National Institute of Education

End Show

Declaring Fixed size arrays


Local array,

Use the Dim statement in a procedure to declare the array.

Dim Stname(9) As String Dim SubjectM(8) As integer

'10 elements. ' 9 elements.

IT Department - National Institute of Education

End Show

Declaring Local Array & Assign values to array

Private Sub Command1_Click() Dim StName(4) As String

Declaration

StName(0) = "Saman"
StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal"
Assign values

End Sub
IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through text box

Private Sub Command1_Click() Dim StName(4) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Text1 = StName(0) Text2 = StName(1) Text3 = StName(2) Text4 = StName(3) End Sub

IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through text box

Private Sub Command1_Click() Dim StName(4) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Text1 = StName(0) Text2 = StName(1) Text3 = StName(2) Text4 = StName(3) End Sub

Text1 text box

IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through text box

Private Sub Command1_Click() Dim StName(4) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Text1 = StName(0) Text2 = StName(1) Text3 = StName(2) Text4 = StName(3) End Sub

Text2 text box

IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through text box

Private Sub Command1_Click() Dim StName(4) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Text1 = StName(0) Text2 = StName(1) Text3 = StName(2) Text4 = StName(3) End Sub

Text3 text box

IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through text box

Private Sub Command1_Click() Dim StName(4) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Text1 = StName(0) Text2 = StName(1) Text3 = StName(2) Text4 = StName(3) End Sub

Text4 text box

IT Department - National Institute of Education

End Show

Declaring & Assigning values to a local array to get Output of the contents of the array through Message Box Private Sub Command1_Click() Dim StName(8) As String StName(0) = "Saman" StName(1) = "Kamal" StName(2) = "Ranil" StName(3) = "Sunil" StName(4) = "Nimal" Temp=MsgBox (StName(0) & " " & StName(1) & " " & StName(2) &_ " &_ StName(3) & " " & StName(4),,Arrays demo) End Sub

IT Department - National Institute of Education

End Show

Dynamic Arrays
Sometimes you may not know exactly how large an array

should be. You may want to have the capability of


changing the size of the array at run time.

A dynamic array can be resized at any time. Dynamic arrays help you to manage memory efficiently.

IT Department - National Institute of Education

End Show

Create a dynamic array

Declare the array with a Public statement (if you want the array to be public) or Dim statement at the module level (if you want the array to be module level), or a Static or Dim statement in a procedure (if you want the

array to be local). You declare the array as dynamic by


giving it an empty dimension list.
End Show

IT Department - National Institute of Education

Dynamic Arrays

Declaration

Dim StName() As String Private Sub Command1_Click() ReDim StName(3) StName(0) = "Kamal" StName(1) = "Ranil" StName(2) = "Sunil" StName(3) = "Anil" MsgBox (StName(0) & " " & StName(1) & " " &_ StName(2) &_ " & StName(3),Arrays Demo) End Sub

IT Department - National Institute of Education

End Show

Dynamic Arrays
Dim StName() As String Define size Private Sub Command1_Click() ReDim StName(3) StName(0) = "Kamal" StName(1) = "Ranil" StName(2) = "Sunil" StName(3) = "Anil" MsgBox (StName(0) & " " & StName(1) & " " &_ StName(2) &_ " & StName(3),Arrays Demo) End Sub

IT Department - National Institute of Education

End Show

Dynamic Arrays
Dim StName() As String Private Sub Command1_Click() ReDim StName(3) StName(0) = "Kamal" StName(1) = "Ranil" Assign values StName(2) = "Sunil" StName(3) = "Anil" MsgBox (StName(0) & " " & StName(1) & " " &_ StName(2) &_ " & StName(3),Arrays Demo) End Sub

IT Department - National Institute of Education

End Show

Dynamic Arrays
Dim StName() As String Private Sub Command1_Click() ReDim StName(3) StName(0) = "Kamal" Display Values StName(1) = "Ranil" StName(2) = "Sunil" StName(3) = "Anil" MsgBox (StName(0) & " " & StName(1) & " " &_ StName(2) &_ " & StName(3),Arrays Demo) End Sub

IT Department - National Institute of Education

End Show

Write a program to input marks of 8 subjects. Calculate total & Display


Private Sub Command1_Click() Dim marks(7) As Integer Dim i As Integer Dim sum As Integer Dim total As Integer For i = 0 To 7 marks(i) = InputBox("Enter " & i & " Marks") Next For i = 0 To 7 total = total + marks(i) Next For i = 0 To 7 Print "Content of " & i & " Element of Array ", marks(i) Next Print "Total is ", total End Sub

IT Department - National Institute of Education

End Show

Private Sub Command1_Click() Dim marks(7) As Integer Dim i As Integer Dim sum As Integer Dim total As Integer For i = 0 To 7 marks(i) = InputBox("Enter " & i & " Marks") Next For i = 0 To 7 total = total + marks(i) Next For i = 0 To 7 Print "Content of " & i & " Element of Array ", marks(i) Next Print "Total is ", total End Sub

IT Department - National Institute of Education

End Show

You might also like