slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Step 1 PowerPoint Presentation

Step 1

53 Views Download Presentation
Download Presentation

Step 1

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. ArraysArray of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same nameArray of Variables:Static/Dynamic ArraysSingle-Dimension/Multi-Dimensional Arrays

  2. Control ArraysA 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 colourAfter 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 ArraySelect ‘Yes’, and the name in the Properties Window becomes optColour(0) for the first control and optColour(1) for the second controlThe 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 attachedIn this example, the 5 option buttons are named optColour(0), optColour(1), optColour(2), optColour(3), optColour(4)

  4. Step 3 Result Step 1 Step 2

  5. Private Sub optColour_Click(Index As Integer)‘Set the colour to match the selected option buttonSelect 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 = vbYellowEnd SelectEnd Sub

  6. Variable ArraysSingle-Dimension ArraysA 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 boxAny 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 upAn 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 Elementof the ArrayThe individual Elements of an Array are treated the same as any other Variable and may be used in any assignment statementThe Subscript (which may also be called an Index) inside the parenthesis, is the position of the Element within the ArraySubscripts 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 statementDim ArrayName( [LowerSubscript To] UpperSubscript) As DataTypeThe 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 IntegerAny Array dimensioned in this way is referred to as a Static ArrayA Static Array may be dimensioned only once in a projectAny attempt to change the size of a Static Array after its first use (project execution) causes the following error message: “Array Already Dimensioned”

  10. Numbers Index Values Need to explicitly initialise the elements of the array with assignment statementsNumbers(0) = 77 Numbers(1) = 68etcRepetition statements can also be used 77 (0) (1) (2) (3) (4) (5) Numbers(0) = 77 68 Numbers(1) = 68 55 Numbers(2) = 55 72 Numbers(3) = 72 79 Numbers(4) = 79 89 Numbers(5) = 89

  11. ArraysArrays occupy space in memoryThe compiler reserves the required amount of space in memory through the declaration statementArrays can be declaredGlobally (Public)Module (Dim/Private)Local (Dim/Static)

  12. ArraysThe 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

  13. 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 1Dim Array(0 TO 9) As Integer Dim Array(1 TO 10) As IntegerAn alternative solution is to use the Option Base statementOption 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

  14. 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

  15. For Each/Next StatementsThere is a need to have a method of reference to each Element in an ArrayA 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 NextElementName

  16. For Each ElementName In ArrayName Statement(s) in Loop NextElementName VB automatically references each Element of the Array, assigns its value to ElementName, and makes one pass through the loopIf the Array has nElements, the loop will execute n timesThe Variable used for ElementName must be a Variant Data Type

  17. With an array named stName, each element of the array is printed Dim vOneName As VariantFor Each vOneName In stName Print vOneName ‘Print one element of the arrayNext vOneNameThe 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 arrayFurthermore, an Exit For Statement may be used within the loop to exit early, just as in a For/Next loop

  18. Using a For…Next Loop Dim X AsInteger For X = LBound(stName) To UBound(stName) Print stName(X) Next X

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. Dynamic Arrays

  25. Dynamic Arrays

  26. Dynamic Arrays

  27. Dynamic Arrays

  28. Dynamic Arrays

  29. Dynamic Arrays