Page 174 - Open Soource Technologies 304.indd
P. 174

Event Driven Programming



                          ReDim Cells(1 To 100, 10) As Integer
                          ...
                          ReDim Preserve Cells(1 To 100, 20) As Integer ‘ This works.
                          ReDim Preserve Cells(1 To 200, 20) As Integer ‘ This doesn’t.
                          Finally, you can destroy an array using the Erase statement. If the array is dynamic, Visual Basic
                          releases the memory allocated for its elements (and you can’t read or write them any longer);
                          if the array is static, its elements are set to 0 or to empty strings.
                          You can use the LBound and UBound functions to retrieve the lower and upper indices. If the
                          array has two or more dimensions, you need to pass a second argument to these functions to
                          specify the dimension you need:
                          Print LBound(Cells, 1) ‘ Displays 1, lower index of 1st dimension
                          Print LBound(Cells) ‘ Same as above
                          Print UBound(Cells, 2) ‘ Displays 20, upper index of 2nd dimension
                          ’ Evaluate total number of elements.
                          NumEls = (UBound(Cells) _ LBound(Cells) + 1) * _
                          (UBound(Cells, 2) _ LBound(Cells, 2) + 1)


                          7.1.4 Arrays within UDTs

                          UDT structures can include both static and dynamic arrays. Here’s a sample structure that contains
                          both types:
                          Type MyUDT
                          StaticArr(100) As Long
                          DynamicArr() As Long
                          End Type

                          ...
                          Dim udt As MyUDT
                          ’ You must DIMension the dynamic array before using it.
                          ReDim udt.DynamicArr(100) As Long
                          ’ You don’t have to do that with static arrays.
                          udt.StaticArr(1) = 1234
                          The memory needed by a static array is allocated within the UDT structure; for example, the
                          StaticArr array in the preceding code snippet takes exactly 400 bytes. Conversely, a dynamic
                          array in a UDT takes only 4 bytes, which form a pointer to the memory area where the actual
                          data is stored. Dynamic arrays are advantageous when each individual UDT variable might host
                          a different number of array items. As with all dynamic arrays, if you don’t dimension a dynamic
                          array within a UDT before accessing its items, you get an error 9—”Subscript out of range”.






                          168                    LOVELY PROFESSIONAL UNIVERSITY
   169   170   171   172   173   174   175   176   177   178   179