Download presentation
Presentation is loading. Please wait.
1
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
2
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)
3
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)
5
Private Sub optColour_Click(Index As Integer) ‘Set the colour to match the selected option button Select Case Index Case 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 = vbYellow End Select End Sub
6
Variable Arrays Single-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
7
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
8
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)
9
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”
10
Need to explicitly initialise the elements of the array with assignment statements Numbers(0) = 77 Numbers(1) = 68 etc Repetition statements can also be used (0) (1) (2) (3) (4) (5) Index Values Numbers 77 68 55 72 79 89 Numbers(0) = 77 Numbers(1) = 68 Numbers(2) = 55 Numbers(3) = 72 Numbers(4) = 79 Numbers(5) = 89
11
Arrays Arrays occupy space in memory The compiler reserves the required amount of space in memory through the declaration statement Arrays can be declared Globally (Public) Module (Dim/Private) Local (Dim/Static)
14
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
17
Arrays Instead 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
18
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
19
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
20
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
21
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 loop are 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
22
Using a For…Next Loop Dim X As Integer For X = LBound(stName) To UBound(stName) Print stName(X) Next X
23
Dynamic Arrays Arrays that grow and shrink at run time are known as Dynamic Arrays or Redimmable Arrays More flexible that static arrays, due to the fact that they can be resized to accommodate new data Can have Public/Form Module or Local level scope
24
Dynamic Arrays –Dim DynamicArray( ) as String –The size of a dynamic array is specified at run time with the keyword ReDim and the memory for the array is then allocated –ReDim DynamicArray(9) –This would allocate 10 elements to the array named DynamicArray The total number of dimensions cannot be changed by ReDim –if the array is a two-dimensional array, it cannot become a three-dimensional array –the first time ReDim is used - the number of dimensions of the array become fixed
25
Dynamic Arrays When ReDim is executed, all values contained in the array are lost Values can be retained by using the keyword Preserve with ReDim –Dim DynamicArray( ) As String –ReDim DynamicArray(9) –ReDim Preserve DynamicArray(19) –Resizes DynamicArray and retains the original values for the first 10 elements in the array
26
Dynamic Arrays Memory allocated for a dynamic array can be deallocated (released) at run time by using the keyword Erase A dynamic array that has been deallocated must be redimensioned with ReDim before it can be used again –Erase can also be used with Static Arrays to initialise all of the array elements to 0
27
Programming Errors in Arrays –Attempting to use ReDim outside a procedure is a syntax error –Attempting to use ReDim on a fixed-size (static) array is a syntax error –Resizing dynamic arrays consumes processor time and can slow a programs execution speed –Attempting to change the total number of dimensions of a dynamic array is a run time error –Using ReDim without Preserve and assuming that the array still contains previous values is a logic error Failure to Preserve array data can result in unexpected loss of data at run time –Accessing a dynamic array that has been deallocated is a run time error
28
Dynamic Arrays
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.