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, Part 2


  • Read Part 1

  • In Part 1 we discussed why a dynamic array class would be nice and looked at the code to create such a class. In this part, we'll focus on how to use the DynamicArray class created in Part 1!

    - continued -

    To use the DynamicArray class, simply create an instance of the class like so:

    Dim objDynArray
    Set objDynArray = New DynamicArray
    

    To add elements to the class instance, simply use the Data property like so:

    objDynArray.Data(4) = "Hello!"
    

    To loop through the array you can use code like:

    Dim i
    For i = LBound(objDynArray.DataArray) to UBound(objDynArray.DataArray)
    'Or, the above line could be replaced by:
    'For i = objDynArray.StartIndex() to objDynArray.StopIndex()
      
      Response.Write i & ".) " & objDynArray.Data(i) & "
    " Next

    Finally, to delete an item, use the Delete method like so:

    Dim objDynArray
    Set objDynArray = New DynamicArray
    
    objDynArray(0) = "Hello"
    objDynArray(2) = "world!"
    objDynArray(1) = "there"
    
    'The array now has the values ("Hello", "there", "world!")
    'Delete the "there" element (index = 1) so we have Hello, world!
    objDynArray.Delete 1
    
    'Now, objDynArray has the values ("Hello", "world!")
    

    With the DynamicArray class you no longer have to worry about constantly Rediming an array or making sure that you have legal array bounds. This class allows you to just slap a value in the array position you want. If the array isn't big enough, it grows. If you want to get rid of an array element, the array compacts itself for you.

    A Note on Performance
    Notice that each time the developer attempts to insert an array element whose position is too big, the array issues a Redim statement. This will lead to a ton of Redim statements if the developer does something like:

    Dim i
    For i = 0 to SomeBigNumber
      objDynArray.Data(i) = SomeValue
    Next
    

    Rather than just adding one element position when Rediming the array in the Data Let Property, why not add a number of elements? This way fewer Redim statements are issued, resulting in fewer memory allocations. Of course if you allocate too many array elements at a time, it will lead to wasted space.

    I hope you found this article interesting and informative, and hopefully you will find the class applicable to your current projects! Feel free to use the DynamicArray class in any of your ASP pages!

    Happy Programming!

  • Read Part 1

  • Download the source code for DynamicArray in text format


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