[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Arrays

Arrays are collections of homogeneous data items identified by the same name, with elements accessed via zero-based indexing. There are two types of arrays: fixed arrays, which have a set size, and dynamic arrays, which can be resized using the ReDim statement. Arrays can be one-dimensional or multi-dimensional, and their declaration and initialization involve specifying data types and subscripts.

Uploaded by

rehna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Arrays

Arrays are collections of homogeneous data items identified by the same name, with elements accessed via zero-based indexing. There are two types of arrays: fixed arrays, which have a set size, and dynamic arrays, which can be resized using the ReDim statement. Arrays can be one-dimensional or multi-dimensional, and their declaration and initialization involve specifying data types and subscripts.

Uploaded by

rehna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Arrays

Arrays are collection of homogenous data item referred to by


the same name. Arrays can hold multiple items. These items are
called elements of the array. Arrays store data of the same data
type. Each element can be referred to by an index. Arrays are
zero based. The index of the first element is zero.

Two types of Arrays

1. Fixed arrays
2. Dynamic arrays

Fixed Arrays
Fixed arrays have a fixed size which cannot be changed at run-time.

Declaration of Arrays

Dim|Public|Private ArrayName(Subscript) As DataType

 Dim, Public,
and Private declare the array and its scope. Using Dim in a
procedure will make the array only available from within that
procedure. Using it in the General Declarations section will make it
available to all procedures in that module. Private has the same effect
and should be used only at the modular level. Using Public will make
the array available throughout the project.
 ArrayName is the name of the array.
 Subscript is the dimensions of the array.
 DataType is any valid data type.

example

Dim a(5) As Integer

Dim b(2 to 5) as integer


Initialization of Arrays
Arrayname(index)=value
Example
Dim a(4) as integer
a(0) = 3
a(1) = 2
a(2) = 1
a(3) = 5
a(4) = 6
The values 3,2,1,5,6 assigned to array a starting from index 0.

Retrieving elements from Array

value=arrayname(index)
Eg.

x=a(2)

msgbox(x) - > 1

eg. displaying all elements from array named a.

for i=0 to 4

print a(i)

next i

output:

Dynamic arrays differ from fixed arrays because a subscript range for
the array elements is not specified when the array is dimensioned.
Instead, the subscript range is set using the ReDim statement.
Syntax for ReDim statement –
Dim arrayname() as datatype
ReDim [Preserve] arrayname(subscripts)
Where,
 The Preserve keyword helps to preserve the data in an existing array, when
you resize it.
 arrayname is the name of the array to re-dimension.
 subscripts specifies the new dimension.

Dim marks() As Integer


ReDim marks(2)
marks(0) = 85
marks(1) = 75
marks(2) = 90

ReDim preserve marks(8)


marks(3) = 80
marks(4) = 76
marks(5) = 92
marks(6) = 99
marks(7) = 79
marks(8) = 75

Array dimensions

1. one dimensional array – has only one subscript


2. two dimensional array – has two subscripts
can be defined as an array of arrays

multidimensional arrays are declared using the Dim keyword:

Dim strBooks(2, 1) As String

The above Visual Basic code creates a two dimensional String array of 3 rows
and 2 columns.

You might also like