You are on page 1of 33

Arrays

Chapter 7 - Visual Basic

Schneider

Outline and Objective


Arrays in Visual Basic
Array and its types Control arrays

Chapter 7 - Visual Basic

Schneider

Array verses Simple Variable


Simple variable is used to store a single value. Array variable is used to represent many values of the same type with one variable name.

Chapter 7 - Visual Basic

Schneider

Elements of an Array
Array Name: A valid variable name for the structure. Subscript or Index : A value that refers to a particular array element. Element: An individual data item within an array. Lower Bound: The first element in an array. Upper Bound: The last element in an array.
Chapter 7 - Visual Basic Schneider 4

Array Declaration
Syntax Dim arrayname( 1 To n) As VarType Or Dim arrayname(higher subscript) As VarType

Chapter 7 - Visual Basic

Schneider

Examples of arrays
Dim month ( 1 To 12) As String Dim score (1 To 30) As Single Dim students (1 To 30 ) As String Dim a(20) As Integer

Chapter 7 - Visual Basic

Schneider

Initializing an Array
Private Sub command_Click() Dim teamName( 1 To 5) As String ' Fill array with World Series Winners teamName(1) = "Red Sox" teamName(2) = "Giants" teamName(3) = "White Sox" teamName(4) = "Cubs" teamName(5) = "Cubs" End Sub

Chapter 7 - Visual Basic

Schneider

Array teamName()
Array Name

teamName( 1 To 5) As String

Red Sox Giants White Sox Cubs

Cubs

teamName(1)
Index

Chapter 7 - Visual Basic

Schneider

Arrays
The most commonly used arrays are onedimensional and two-dimensional

Chapter 7 - Visual Basic

Schneider

One-Dimensional Arrays
A one-dimensional array is simply a row (or column) of variables Each element in an array is identified by a subscript, which Visual Basic assigns to the variable when the array is created

Chapter 7 - Visual Basic

Schneider

10

One-Dimensional Arrays (continued)


You refer to each variable in an array by its name and the variables subscript

Figure 10-3: Names of the variables in a one-dimensional array named states


Chapter 7 - Visual Basic Schneider 11

One-Dimensional Arrays (continued)


Declaring a one-dimensional array
Version 1 {Dim | Private} arrayname(highestSubscript) As datatype Version 2

{Dim | Private} arrayname() As datatype = {initialValues}


Chapter 7 - Visual Basic Schneider 12

One-Dimensional Arrays (continued)


Examples of declaring an array
Dim cities(3) As String Private states() As String = {Hawaii, Alaska, Maine}

Chapter 7 - Visual Basic

Schneider

13

Storing Data in a OneDimensional Array


In most cases, you use an assignment statement to enter data into an existing array Syntax: arrayname(subscript) = value Examples
cities(0) = Madrid cities(1) = Paris cities(2) = Rome
Chapter 7 - Visual Basic Schneider 14

Multi-Dimensional Arrays
Visual basic will allow us up to 60 dimensions! To define 2-dimensional array, the dim statement specifies the number of rows and columns in the array. Syntax:- Dim ArrayName([LowerLimit to] UpperLimit, [LowerLimit to] UpperLimit) As Datatype Examples: Dim strName(2,3) As String Dim strName as (0 to 2, 0 to 3) As String

Types of Arrays
Fixed-Size Arrays
Public Arrays Module-level Arrays Local Arrays

Dynamic Arrays

Chapter 7 - Visual Basic

Schneider

16

Fixed-Size Arrays
These are the arrays whose dimension is specified while declaring the array. They are of following 3 types:a) Public Arrays: They are declared and accessed just like public variables and are available to all modules in a project. Example: Public address(100) as String
Chapter 7 - Visual Basic Schneider 17

Fixed-Size Arrays
b) Module-level Arrays: These are available to the module in which they appear. Example: Dim color(40) as Integer c) Local Arrays: Used in sub-procedures and are accessible only to the procedure which declares them. Example: Public Sub getID() Dim empNum(30) As Long End Sub
Chapter 7 - Visual Basic Schneider 18

Dynamic Arrays
These are the arrays whose dimension is not specified while declaring. An Array that changes the size during runtime is called a dynamic array.

Dynamic Arrays
In the declaration Section: Dim arr(1 to 15) As String Within a procedure: ReDim arr(5)

An array dimensioned in this way is referred to as a dynamic array because the number of elements may change during program execution by using the ReDim Statement. Initially Dynamic array arr() requires 15*2=30 bytes because each integer occupies 2 bytes. But after the execution of the second ReDim, the size of the array changes to 5*2=10 bytes.
Chapter 7 - Visual Basic Schneider 20

Dynamic Arrays
When the ReDim Statement is executed, the values that were stored in the dynamic array are lost. If we want to preserve some of the values of the array, we must use Preserve Keyword that will resize only the last array dimension.
ReDim Preserve arr(5)
Chapter 7 - Visual Basic Schneider 21

Passing an Array
An array can be passed to another procedure by reference.

Chapter 7 - Visual Basic

Schneider

22

Example of Passing an array:


Private Sub cmddisplay_Click() ' Pass array to subprogram and function Dim score(1 To 5) As Integer Call FillArray(score( ) ) Passing array score picAverage.Cls picAverage.Print Average is"; Sum(score( ) ) / 5 End Sub

Chapter 7 - Visual Basic

Schneider

23

Array Score is passed to a Subprogram


Private Sub FillArray(s( ) As Integer) ' Fill array with scores s(1) = 85 s(2) = 92 This array is pointing to the same s(3) = 75 location as array score s(4) = 68 s(5) = 84 End Sub
Chapter 7 - Visual Basic Schneider 24

Array Score is passed to a Function


Private Function Sum(s ( ) As Integer) As Integer Dim total As Integer, index As Integer ' Add up scores total = 0 For index = 1 To 5 total = total + s(index) Next index Sum = total End Function
Chapter 7 - Visual Basic Schneider 25

Control Array
A means of constructing arrays of text boxes, labels, and command buttons. At least one element of a control array must be created when the form is designed. The remaining elements can be created either during form design, or with the Load statement.
Chapter 7 - Visual Basic Schneider 26

Creating Control Array during Form Design Add one instance of the desired control to the form Set the Index property to a number Set any other properties that will be common to all other elements Click on the control and then press Ctrl+C Press Ctrl + V, to create the next element
Chapter 7 - Visual Basic Schneider 27

Control Array
All the properties of the first element are passed to other elements of the control array including the Top and Left properties. The only property that differs from first element is the Visible property. The Load statement sets the Visible property to False.
Chapter 7 - Visual Basic Schneider 28

Control Array Event Procedures:


Even though we may have many elements in the txtBox( ) control array, we will have just one txtBox_GotFocus event procedure to deal with. The value of Index property for the elements of the control array must be passed. Example:
Private Sub txtBox_GotFocus (Index As Integer)
Chapter 7 - Visual Basic Schneider 29

Creating Control Array at Run Time:


You can create elements of a control array via Load statement at run time.

Chapter 7 - Visual Basic

Schneider

30

Creating Control Array at Run Time:


The standard unit of measurement is called Twip. To place a new element of a control array, adjust the Top and Left properties during run time. Set the Visible property to True.

Chapter 7 - Visual Basic

Schneider

31

The location and size of a control

Chapter 7 - Visual Basic

Schneider

32

Example ( Creating control array during run time)


Private Sub Form_Load() Dim i As Integer, monthNames As String monthNames = "FebMarAprMayJunJulAugSepOctNovDec" For i = 1 To 11 Load lblMonth(i) Load txtInfo(i) blMonth(i).Top = lblMonth(i - 1).Top + txtInfo(0).Height txtInfo(i).Top = txtInfo(i - 1).Top + txtInfo(0).Height lblMonth(i).Caption = Mid(monthNames, 3 * i - 2, 3) lblMonth(i).Visible = True txtInfo(i).Visible = True Next i End Sub
33

You might also like