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

Event Driven Programming



                          7.1  Declaring Arrays

                          Arrays occupy space in memory. The programmer specifies the array type and the number of
                          elements required  by the array so that the compiler may reserve the appropriate amount of
                          memory. Arrays may be declared as Public (in a code module), module or local. Module arrays
                          are declared in the general declarations using keyword Dim or Private. Local arrays are declared
                          in a procedure using Dim or Static. Array must be declared explicitly with keyword “As”.
                          There are two types of arrays in Visual Basic namely:
                             • Fixed-size array: The size of array always remains the same-size doesn’t change during the
                               program execution.
                             • Dynamic array: The size of the array can be changed at the run time- size changes during
                               the program  execution.
                          7.1.1 Fixed-sized Arrays
                          When an upper bound is specified in the declaration, a Fixed-array is created. The upper limit
                          should always be within the range of long data type.
                          Declaring a fixed-array
                          Dim numbers (5) As Integer
                          In the above illustration, numbers is the name of the array, and the number 6 included in the
                          parentheses is the upper limit of the array. The above declaration creates an array with 6
                          elements, with index numbers running from 0 to 5.
                          If we want to specify the lower limit, then the parentheses should include both the lower and
                          upper limit along with the To keyword. An example for this is given below.
                          Dim numbers (1 To 6 ) As Integer
                          In the above statement, an array of 10 elements is declared but with indexes running from 1 to 6.
                          A public array can be declared using the keyword Public instead of Dim as shown below.
                          Public numbers (5) As Integer

                          7.1.2 Multidimensional Arrays
                          Arrays can have multiple dimensions. A common use of multidimensional arrays is to represent
                          tables of values consisting of information arranged in rows and columns. To identify a particular
                          table element, we must specify two indexes: The first (by convention) identifies the element’s row
                          and the second (by convention) identifies the element’s column.
                          Tables or arrays that require two indexes to identify a particular element are called two dimen-
                          sional arrays. Note that multidimensional arrays can have more than two dimensions. Visual
                          Basic supports at least 60 array dimensions, but most people will need to use more than two or
                          three dimensional-arrays.
                          The following statement declares a two-dimensional array 50 by 50 array within a procedure.
                          Dim AvgMarks (50, 50)
                          It is also possible to define the lower limits for one or both the dimensions as for fixed size
                          arrays. An example for this is given here.
                          Dim Marks (101 To 200, 1 To 100)
                          An example for three dimensional-array with defined lower limits is given below.
                          Dim Details (101 To 200, 1  To 100, 1 To 100)




                          166                    LOVELY PROFESSIONAL UNIVERSITY
   167   168   169   170   171   172   173   174   175   176   177