When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Tuesday, March 28, 2000

Dynamic Arrays Made Easy


If you've used VBScript's arrays before, you're likely aware that you can dynamically resize arrays using the Redim statement. (To learn more, see the technical documentation for Redim.) Sometimes this can be a pain, though. For example, imagine that you wanted to read each line of a text file into an array. Since you do not know the number of lines in the file initially, there is no way to create a preset array size. Rather, you would use code like:

- continued -

Loop, reading in each line of the array
  Add a new element to the array
  Store the line in the array

While this is feasible, you, the developer, have to Redim the array at each iteration. Now, imagine that you wanted to delete a particular element from the array and then compact the array? Ugh. This would involve moving elements backwards through the array then issuing another Redim statement to compact the array.

What would be nice would be to have an easy-to-use dynamic array class. This class would have the same functionality of a regular array, but would be dynamic in nature. That is, the developer working with this class could simply say:

MyArray(N) = Something

and not have to worry if the array MyArray was of the proper dimensions or not. Also, it would be nice if the developer could easily delete an element from the array and compact it with code like:

MyArray.Delete N

If you are new to classes, I highly recommend you read some of the great articles on VBScript classes before continuing on with this article:

The DynamicArray Class
This class needs only one member variable, the actual array that is used to store the data (named aData). In the Initialize event, aData is Redimed as an array with one element. Note that arrays are indexed starting at zero, not one, in this class.

The class contains two Property Get statements: Data and DataArray. Data returns a particular array element while DataArray returns the entire array. A single Property Let statement exists for the Data property. This array does the grunt work of Rediming the array, if needed.

The DynamicArray class contains three methods: StartIndex, StopIndex, and Delete. StartIndex simply returns the starting array index (like LBound) while StopIndex returns the array's current greatest possible index (like UBound). The third and final method, Delete, removes a particular element from the array and then compacts the array.

In Part 2 we'll look at how to use this array to create truly dynamic arrays. The remainder of this part contains the source code for the DynamicArray class.

  • Read Part 2


    Class DynamicArray
      '************** Properties **************
      Private aData
      '****************************************
    
      '*********** Event Handlers *************
      Private Sub Class_Initialize()
        Redim aData(0)
      End Sub
      '****************************************
    
      '************ Property Get **************
      Public Property Get Data(iPos)
        'Make sure the end developer is not requesting an
        '"out of bounds" array element
        If iPos < LBound(aData) or iPos > UBound(aData) then
          Exit Property    'Invalid range
        End If
    
        Data = aData(iPos)
      End Property
    
      Public Property Get DataArray()
        DataArray = aData
      End Property
      '****************************************
    
      '************ Property Let **************
      Public Property Let Data(iPos, varValue)
        'Make sure iPos >= LBound(aData)
        If iPos < LBound(aData) Then Exit Property
    
        If iPos > UBound(aData) then
          'We need to resize the array
          Redim Preserve aData(iPos)
          aData(iPos) = varValue
        Else
          'We don't need to resize the array
          aData(iPos) = varValue
        End If
      End Property
      '****************************************
    
    
      '************** Methods *****************
      Public Function StartIndex()
         StartIndex = LBound(aData)
      End Function
    
      Public Function StopIndex()
         StopIndex = UBound(aData)
      End Function
    
      Public Sub Delete(iPos)
         'Make sure iPos is within acceptable ranges
         If iPos < LBound(aData) or iPos > UBound(aData) then
           Exit Sub    'Invalid range
         End If
    
         Dim iLoop
         For iLoop = iPos to UBound(aData) - 1
           aData(iLoop) = aData(iLoop + 1)
         Next
    
         Redim Preserve aData(UBound(aData) - 1)
      End Sub
      '****************************************
    End Class
    


  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article