You are on page 1of 7

VB.

NET
ARRAY
ARRAY CONCEPTS

An array is a collection of values of identical data type referenced under one


name.

The variables in an array are called array elements, which are accessed using a
single name and index number representing the position of the element within
the array.
DECLARING AN ARRAY
Dim ColorRed() As String = New String() {“Apple”, “Strawberry”, “Tomato”, “Cherry”}
Dim ColorYellow() As String = New String() {“Banana”, Mangoes”, “Lemon”, “Corn”}
Dim ColorOrange(4) As String
ColorOrange(0) = “Orange”
ColorOrange(1) = “Papaya”
ColorOrange(2) = “Carrot”
ColorOrange(3) = “Pumpkin”

Dim ColorViolet(4) As String


ColorViolet(0) = “Grape”
ColorViolet(1) = “Eggplant”
ColorViolet(2) = “Raisin”
ColorViolet(3) = “Plum”

Syntax for Declaring Array: Dim ArrayName(No of Elements) As Data Type


Index Number/Array Subscript – used to assign values to each element in an
Array. Always starts with 0.
RETRIEVING CONTENTS OF
AN ARRAY
Dim ColorRedElementList As String = “ ”
For lngPosition = LBound(ColorRed) To
UBound(ColorRed)
ColorRedElementList = ColorRedElementList &
ColorRed(lngPosition) & “, ”
Next
MsgBox(ColorRedElementList)

 LBound – returns the lowest index of the array.


 UBound – returns the highest index of the array.
ADDING ELEMENTS TO AN
ARRAY
Dim ColorRedElementList As String = “ ”
Dim ArrayCounter As Integer = 0
Do While ArrayCounter < ColorRed.Length
ColorRedElementList = ColorRedElementList &
ColorRed(ArrayCounter) & “, ”
ArrayCounter = ArrayCounter + 1
Loop
MsgBox(ColorRedElementList)
GLOBAL VARIABLES

 These are declared in the general declarations part of the code. They are
outside any subroutine or functions but still within the class.

 These variables have a Public access modifier which means that they can
be used by all elements in the project.
ASSESSMENTS

1. Answer Seatwork 1 and Worksheet 1


2. Study the lesson about Array for a VB activity next
meeting.

You might also like