<pedrocorreia.net ⁄>
corner
<mySearch ⁄> <mySearch ⁄>

corner
 
corner
<mySnippets order="rand" ⁄> <mySnippets order="rand" ⁄>

corner
 
corner
<myContacts ⁄> <myContacts ⁄>

<email ⁄>


pc@pedrocorreia.net

<windows live messenger ⁄>


pedrojacorreia@hotmail.com

<myCurriculum type="pdf" ⁄>


Download
corner
 
corner
<myBlog show="last" ⁄> <myBlog show="last" ⁄>

corner
 
corner
<myNews show="rand" ⁄> <myNews show="rand" ⁄>

corner
 
corner
<myNews type="cat" ⁄> <myNews type="cat" ⁄>

corner
 
corner
<myQuote order="random" ⁄> <myQuote order="random" ⁄>

A desonra é como cicatriz numa árvore; aumenta todos os dias.
corner
 
corner
<myPhoto order="random" ⁄> <myPhoto order="random" ⁄>

<pedrocorreia.net ⁄>
corner
 
corner
<myAdSense ⁄> <myAdSense ⁄>

corner
 
corner
<myVisitorsMap ⁄> <myVisitorsMap ⁄>

corner
 
 

<Classic ASP Array Redim Preserve ⁄ >




clicks: 26322 26322 2008-08-10 2008-08-10 goto mySnippets mySnippets asp  Download  Bookmark This Bookmark This



When compared to PHP4, manipulating arrays in Classic ASP it's further more difficult (well not just in arrays, but I won't start a flame war :)), even for the built-in functions, PHP has a broader set of functions. Please note that I'm not bashing Classic ASP! Classic ASP has some points on which's better than PHP, nowadays I still code on Classic ASP, not very often, but it still happens.

One particular thing is adding elements to an array, a quite basic and common action you do during your scripting, we can't just add the element at our arrays last index+1, that will cause Subscript out of range error.

To add elements to an array you have to Redim the array, this is, you have to reallocate storage space. You can get more detailed information about Redim in this link.

However if you simple use Redim all your array will be lost, since Redim by itself creates a new array but it doesn't keep its content, so you have to use Redim Preserve, i.e, it will create at the same a new array, however it will preserve old values and will add more elements as you specified, as you can easily guess it'll be a more expensive operation, since it'll make a copy of the old array and create a new one.

I'll take an OOP approach in this snippet, yes, Classic ASP has some OOP functionalities, however as you know, it's not really a true OOP language, for example you don't have inheritance, polymorphism, but for example you have encapsulation, class methods, properties, etc. I'll try to explain this a future snippet by giving more examples.


ArrayRedim.asp: this class's responsible for all array manipulations.
  1. <%
  2. ''' <summary>
  3. ''' This class allow us to add elements to an Array
  4. ''' </summary>
  5. '''<author>pedrocorreia.net</author>
  6. Class ArrayRedim
  7.  
  8. 'our auxiliary variable to know the array last valid position,
  9. 'this is just to prevent to use always UBound
  10. Private count_
  11. 'our array
  12. Private arr_content
  13.  
  14.  
  15. ''' <summary>
  16. '''' Initializing Class Method, just like a constructor
  17. '''' Happens each time class's initialized
  18. ''' </summary>
  19. Private Sub Class_Initialize()
  20. Count=0
  21. arr_content=Array()
  22. End Sub
  23.  
  24. ''' <summary>
  25. '''' Terminate Class Method
  26. '''' Happens each time class's terminated or out of scope
  27. ''' </summary>
  28. Private Sub Class_Terminate()
  29. Clear
  30. End Sub
  31.  
  32. ''' <summary>
  33. ''' Private Method
  34. '''
  35. ''' Allocate necessary memory to
  36. ''' add elements to an array
  37. ''' </summary>
  38. ''' <param>Number of elements</param>
  39. Private Sub Allocate(num)
  40. ReDim Preserve arr_content(Count+num)
  41. 'set new array length
  42. Count=Count+num
  43. End Sub
  44.  
  45. ''' <summary>
  46. ''' Add Element
  47. ''' </summary>
  48. ''' <param>Element</param>
  49. Public Default Sub AddElement(element)
  50. 'allocate one element in our array
  51. Allocate 1
  52. 'add element to last valid position
  53. arr_content(Count-1) = element
  54. End Sub
  55.  
  56. ''' <summary>
  57. ''' Add Elements
  58. ''' </summary>
  59. ''' <param>Array Elements</param>
  60. Public Sub AddElements (arrElems)
  61. Dim start_at
  62. start_at=Count
  63.  
  64. if IsArray(arrElems) Then
  65. 'pre-allocate all necessary memory
  66. Allocate UBound(arrElems)
  67.  
  68. For Each elem In arrElems
  69. 'add element
  70. arr_content(start_at)=elem
  71. 'increment position to where we'll add our element
  72. start_at=start_at+1
  73. Next
  74. End If
  75. End Sub
  76.  
  77. ''' <summary>
  78. ''' Clear Resources
  79. ''' </summary>
  80. Public Sub Clear()
  81. Erase arr_content
  82. Count=0
  83. End Sub
  84.  
  85. ''' <summary>
  86. ''' Get Array
  87. ''' </summary>
  88. ''' <param>Array</param>
  89. Public Property Get ArrayRedim()
  90. ArrayRedim = arr_content
  91. End Property
  92.  
  93. ''' <summary>
  94. ''' Print array content
  95. ''' </summary>
  96. ''' <returns>String</returns>
  97. Public Function ToString()
  98. If Count=0 then
  99. ToString="EmptyArray<br/>"
  100. exit function
  101. End If
  102. ToString=Join(arr_content,"<br/>")
  103. End Function
  104.  
  105. ''' <summary>
  106. ''' Getter
  107. ''' Number of Elements in Array
  108. ''' </summary>
  109. ''' <returns>Integer</returns>
  110. Public Property Get Count()
  111. Count=count_
  112. End Property
  113.  
  114. ''' <summary>
  115. ''' Private Method
  116. '''
  117. ''' Setter
  118. ''' Number of Elements in Array
  119. ''' </summary>
  120. ''' <param>Integer</param>
  121. Private Property Let Count(value)
  122. count_=value
  123. End Property
  124.  
  125. End Class
  126. %>








An example file
  1. <!-- #include file ="ArrayRedim.asp" -->
  2. <%
  3.  
  4. Dim redim_array
  5. Set redim_array = new ArrayRedim
  6.  
  7. redim_array.AddElement "1st Element"
  8. redim_array.AddElement "2nd Element"
  9. redim_array.AddElement "3rd Element"
  10. redim_array "4th Element"
  11.  
  12. 'Print array content
  13. Response.Write redim_array.ToString
  14. 'Clear Array
  15. redim_array.Clear
  16. 'Print array content
  17. Response.Write redim_array.ToString
  18.  
  19. Response.Write "<br/><br/>"
  20.  
  21. 'add a few more elements
  22. redim_array "5th Element"
  23. redim_array "6th Element"
  24. redim_array "7th Element"
  25. 'Print array content, don't forget that
  26. 'we previously cleared our array
  27. Response.Write redim_array.ToString
  28.  
  29. Response.Write "<br/><br/>"
  30.  
  31. 'let's just add a few more elements
  32. 'only this time we'll add multiple element at the same time
  33. 'for that just send an Array variable
  34. redim_array.AddElements Array("8th Element","9th Element","10th Element")
  35. 'Print array content
  36. Response.Write redim_array.ToString
  37.  
  38. 'get a copy of the array to do something else ...
  39. dim new_array
  40. new_array=redim_array.ArrayRedim
  41.  
  42. 'clear our ArrayRedim object
  43. Set redim_array=nothing
  44.  
  45. 'we cleared our object, but since we saved its array
  46. 'to another variable, now we can still use it like a regular array
  47. Response.Write "<br/>Array Length: " & Ubound(new_array)+1
  48.  
  49. %>



The file above will output the following content:
1st Element
2nd Element
3rd Element
4th Element
EmptyArray



5th Element
6th Element
7th Element



5th Element
6th Element
7th Element
8th Element
9th Element
10th Element
Array Length: 6





If you have any doubt or found any error, please drop me an email.









clicks: 26322 26322 2008-08-10 2008-08-10 goto mySnippets mySnippets asp  Download  Bookmark This Bookmark This