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

Unit 7: Arrays in Visual Basic



               7.2 ReDim and Preserve Function

               After an array has been declared, its size (but not its type) can be changed with a statement of
               the form
               ReDim arrayName(m)

               where arrayName is the name of the already declared array and m is an Integer literal, variable,
               or expression. Note: Since the type cannot be changed, there is no need for an “As dataType”
               clause at the end of the ReDim statement.

               Visual Basic allows you to declare an array without specifying an upper bound with a statement
               of the form

               Dim arrayName() As varType
               Later, the size of the array can be stipulated with a ReDim statement.

               The ReDim statement has one shortcoming: It causes the array to lose its current contents. That
               is, it resets all String values to Nothing and resets all numeric values to 0. This situation can be
               remedied by following ReDim with the word Preserve. The general form of a ReDim Preserve
               statement is
               ReDim Preserve arrayName(m)

               Of course, if you make an array smaller than it was, data in the eliminated elements will be lost.


                              We can use the ReDim statement repeatedly to change the number of elements
                              and dimensions in an array. However, we can’t declare an array of one data
                              type and later use ReDim to change the array to another data type, unless the
                              array is contained in a Variant.


                              1: The following program reads the names of the winners of Super Bowl games
                                from a file and places them into an array. The user can type a team’s name
                                into a text box and then display the numbers of the Super Bowl games won
                                by that team. The user has the option of adding winners of subsequent
                                games to the array of winners. The program uses the text file
                                SBWINNERS.TXT whose lines contain the names of the winners in order.
                                That is, the first four lines of the file contain the names Packers, Packers,
                                Jets, and Chiefs.
               Dim teamName() AsString
               Dim upperBound As Integer
               Private Sub frmBowl_Load(...) HandlesMyBase.Load
                 Dim sr As IO.StreamReader = IO.File.OpenText(“SBWINNERS.TXT”)
                 Dim name As String, numLines As Integer
                 ‘Count number of lines in the file and assign it minus one to upperBound
                 numLines = 0
                 Do While (sr.Peek <> -1)


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