Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object...

Post on 21-Dec-2015

223 views 0 download

Tags:

transcript

Arrays

Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and

methods), that have the same name

Array of Variables:Static/Dynamic Arrays

Single-Dimension/Multi-Dimensional Arrays

Control Arrays

A group of controls that all have the same name (use with option buttons and check boxes)

All controls in an array must be the same Class (a prototype for an object indicating the properties and methods)

An advantage of using a Control Array, rather than independent controls, is that the controls share one Click

Event (in the arrays Click Event, a Case Structure can be used to determine which option button or check box is

selected)

A group of 5 option buttons, to allow the user to choose a colour

After creating the 5 controls (in a Frame Group), set the Name Property of the first option button to optColour, furthermore, change the Name Property of the second option button to optColour: At this point a Message Box will appear, asking you if you want to create a

Control Array

Select ‘Yes’, and the name in the Properties Window becomes optColour(0) for the first control and optColour(1) for the second

control

The number inside the parenthesis is called an Index and is used to refer to the specific control within the Array; and all subsequent controls,

given the same name, will automatically have an index attached

In this example, the 5 option buttons are named optColour(0), optColour(1), optColour(2), optColour(3), optColour(4)

Private Sub optColour_Click(Index As Integer)‘Set the colour to match the selected option button

Select Case IndexCase 0 ‘First button is selected

Form1.BackColor = vbBlue Case 1 ‘Second button is selected

Form1.BackColor = vbBlack Case 2 ‘Third button is selected

Form1.BackColor = vbRed Case 3 ‘Fourth button is selected

Form1.BackColor = vbWhite Case 4 ‘Fifth button is selected

Form1.BackColor = vbYellowEnd SelectEnd Sub

Variable ArraysSingle-Dimension Arrays

A Variable Array can contain a list of values, similar to a List Box or a Combo Box; (VB actually stores the List Property of a List Box or a Combo Box in an Array); therefore, think of an

array as a list box without the box

Any time a series of variables (multiple values) needs to be kept (stored) for later processing, such as re-ordering, calculating, or printing, an Array needs to be set up

An Array is a series of individual variables, all referenced by the same name; sometimes Arrays are referred to as Tables or

Subscripted Variables

The same notation is used with Variable Arrays as with Control Arrays, therefore, in an array for storing names, called stName, you

may have stName(0), stName(1), stName(2), ….

Each individual Variables is called an Element of the Array

The individual Elements of an Array are treated the same as any other Variable and may be used in any assignment statement

The Subscript (which may also be called an Index) inside the parenthesis, is the position of the Element within the Array

Subscripts may be constants, variables, or numeric expressions; although the Subscripts must be Integers, VB will round any Non-

Integer Subscript

To specify the number of Elements in an Array, you need to use the Dim statement

Dim ArrayName( [LowerSubscript To] UpperSubscript) As DataType

The Dim statement allocates storage for the specified number of Elements and initialises each numeric variable to zero (0);

(in the case of String Arrays, each Element is set to an empty string [zero characters])

Furthermore, it is not necessary to specify the Lower Subscript value; if no value is set for the Lower Subscript,

then the lowest Subscript is zero (0)

Dim stName(0 To 25) As String Dim cBalance(10) As Currency

Dim iCollected(iCount) As Integer

Any Array dimensioned in this way is referred to as a Static Array

A Static Array may be dimensioned only once in a project

Any attempt to change the size of a Static Array after its first use (project execution) causes the following error message:

“Array Already Dimensioned”

Need to explicitly initialise the elements of the array with assignment statements

Numbers(0) = 77 Numbers(1) = 68

etcRepetition statements can also be used

(0)

(1)

(2)

(3)

(4)

(5)

Index Values Numbers776855727989

Numbers(0) = 77

Numbers(1) = 68

Numbers(2) = 55

Numbers(3) = 72

Numbers(4) = 79

Numbers(5) = 89

Arrays

Arrays occupy space in memory

The compiler reserves the required amount of space in memory through the declaration statement

Arrays can be declaredGlobally (Public)

Module (Dim/Private)Local (Dim/Static)

Arrays

The LBound function returns the lower bound (the lowest numbered index value) of the array

The UBound function returns the upper bound (the highest

numbered index value) of the array

ArraysInstead of having Array(0), there maybe a need to refer to the

first element of an array as Array(1)

This means changing the Lower Bound index value of the array from, the default, 0 to 1

Dim Array(0 TO 9) As Integer Dim Array(1 TO 10) As Integer

An alternative solution is to use the Option Base statement

Option Base sets the Lower Bound index value to 0 or 1 only and is placed in [General] [Declarations]

This introduces the concept of Direct Reference for elements in an array

Option Base

set in General Declarations

can be used to set/change the lower bound to 0 or 1 ( no value other that these)

Array(1) instead of Array(0) for the first element

Option Base must be placed before the array declaration

For Each/Next Statements

There is a need to have a method of reference to each Element in an Array

A For/Next Loop can be used, however, another useful looping construct is the For Each/Next Loop; the significant

advantage of using the For Each/Next Loop,is that the Subscripts of the Array do not have to be manipulated

For Each ElementName In ArrayName

Statement(s) in Loop

Next ElementName

VB automatically references each Element of the Array, assigns its value to ElementName, and makes one pass

through the loop

If the Array has n Elements, the loop will execute n times

The Variable used for ElementName must be a Variant Data Type

For Each ElementName In ArrayName

Statement(s) in Loop

Next ElementName

With an array named stName, each element of the array is printed

Dim vOneName As Variant

For Each vOneName In stName Print vOneName ‘Print one element of the array

Next vOneName

The For Each/Next loop will execute if the array has at least one element; All the statements within the body of the loopare executed for the each element in the array

Furthermore, an Exit For Statement may be used within the loop to exit early, just as in a For/Next loop

Using a For…Next Loop

Dim X As Integer

For X = LBound(stName) To UBound(stName)

Print stName(X)

Next X

User-Defined Data Types

Integer, String, Currency, etc…….. are what are known as VB data types

You can define your own data types by combining multiple fields of data into a single unit; A User-Defined Data Type can be used to combine several fields of related information

For example, a Product Data Type might contain a Product Description, a Product Number, Quantity, and Price

The fields can be combined into a User-Defined Data Type by using the Type and End Type Statements

Type NameOfNewDataTypeList of Fields

End Type

Type ProductstDescription As StringstProductNumber As StringiQuantity As IntegercPrice As Currency

End Type

Once you have created your own Data Type, you may use it to declare variables just as you would use any other Data Type

Type Statements can appear only at the Module Level in the General Declarations section of a Standard Code Module or

a Form Module

When Type Statements are placed in the Standard Code Module, they are Public by default; If Type Statements are placed at the Form/Module Level, they must be declared as

Private

Example of a Type Statement in a Form Module:

Private Type …… ……….. End Type