Professional Documents
Culture Documents
Schneider
Schneider
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
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
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
Schneider
Array teamName()
Array Name
teamName( 1 To 5) As String
Cubs
teamName(1)
Index
Schneider
Arrays
The most commonly used arrays are onedimensional and two-dimensional
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
Schneider
10
Schneider
13
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
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.
Schneider
22
Schneider
23
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
Schneider
30
Schneider
31
Schneider
32