
|
Implementing Dynamic Arrays of Objects By Donnell DeLeon Smith |
Rating: 3.5 out of 5 Rate this article
|
email this article to a colleague
suggest an article
|
Introduction
Within any application there eventually is a need to use arrays of data. We find we need the data to hold objects in order to simplify the logic we will be implementing, as well as to make our code more flexible and scalable.
Using a class that encompasses an array of objects, which itself holds and array of objects, we find that the topmost class allows us to acquire a simplified means of sorting, filtering, and keeping track of data items, as well as the values of those data items.
In this article I have cut out the majority of the methods that perform the sorting and filtering to facilitate the learning process. These simplified classes will allow you to quickly grasp and learn to use a dynamic array that holds a dynamic array of objects (both dynamic arrays being objects themselves). Please keep in mind that this code is for Active Server Pages 3.0 (the version of ASP that is part of IIS 5.0, which ships with Windows 2000).
The Code
The deepest layer of our structure is a simple class consisting of any attributes you may need to hold.
'*********** innermost class ***************
'Item Value Class
Class item_value_class
Private c_item_value
' handle value (let/get)
public property let value(byval data)
c_item_value = data
end property
public property get value()
value = c_item_value
end property
end class
'******************************************
Notice this class has one attribute with two properties to allow for writing and reading to the attribute. This is standard and is nothing special.
We now move to the next layer where we encounter our first dynamic array.
'********* middle class ***********************
Class item_class
Private c_item
Private c_values() 'array of all values for item
Private c_size 'determines max possible values for item
' handle item (let/get)
public property let item(byval data)
c_item = data
end property
public property get item()
item = c_item
end property
'*************************************************
'************* methods ***************************
'*************************************************
'*********** Event Handlers *************
Private Sub Class_Initialize()
Redim c_values(0)
Set c_values(0) = New item_value_class
c_size=0
End Sub
' kill all objects in aData and free mem
Private Sub Class_Terminate()
Dim x
for x=0 to ubound(c_values)
Set c_values(0) = nothing
next
c_size=0
erase c_values
End Sub
'****************************************
Public Function getValue(iPos)
getValue = c_values(iPos).value
End Function
Public Function putValue(iPos,data)
if iPos>c_size then
Redim preserve c_values(iPos)
for x = c_size+1 to ubound(c_values)
set c_values(x)= New item_value_class
next
c_size=iPos
end if
c_values(iPos).value=cstr(data)
End Function
Public Property get numbervalues()
numbervalues=c_size
end property
Public property get allvalues()
allvalues=c_values
end property
Public property let allvalues(ByRef temp)
Dim x
Dim sizetochange
sizetochange=ubound(temp)
if sizetochange < c_size then
for x=sizetochange+1 to ubound(c_values)
set c_values(x)=nothing
next
Redim preserve c_values(sizetochange)
else
Redim preserve c_values(sizetochange)
for x = c_size+1 to ubound(c_values)
set c_values(x)= New item_value_class
next
end if
c_size=sizetochange
for x=0 to sizetochange
c_values(x).value = temp(x).value
next
end property
end class
'******************************************
There is more "meat" to this class than the deepest layer. In this class we see the instantiation of an array consisting of objects of our value class. Notice whenever we add a value with the putValue method our array is automatically increased and instantiated. There is a special property I call "allvalues" that creates a reference point to the array this class holds. For all values the GET property returns the reference to the array, while the LET property allows us to set another array of objects of our value class equal to another. The allvalues properties become important in our topmost layer that I call the "Dynamic_Object_Array_Class."
'******** topmost class *********************
Class Dynamic_Object_Array_Class
'************** Properties **************
Private aData()
Private c_size
'****************************************
'*********** Event Handlers *************
Private Sub Class_Initialize()
Redim aData(0)
Set aData(0) = New item_class
c_size=0
c_numberlinks=1
End Sub
' kill all objects in aData and free mem
Private Sub Class_Terminate()
Dim x
for x=0 to ubound(aData)
Set aData(0) = nothing
next
c_size=0
erase aData
End Sub
'****************************************
Public Property Get allitems()
allitems = aData
end property
Public Property Get getsize()
getsize = c_size
end property
Public Function push()
c_size = c_size+1
Redim preserve aData(c_size)
set aData(c_size)= New item_class
push=c_size
End Function
Public Function pop(ipos)
Dim tempcount
pop=aData(ipos).item
if ipos < c_size then
for tempcount=ipos to (c_size - 1)
aData(tempcount).item = aData(tempcount+1).item
aData(tempcount).allvalues = aData(tempcount+1).allvalues
next
end if
set aData(c_size)= nothing
c_size = c_size-1
Redim preserve aData(c_size)
End Function
End Class
'********** end topmost class ***************
The topmost class implements a push- and a pop method to automatically increase and decrease the array of objects of our item class that the topmost class holds. Notice that the previous class' allvalues method is implemented in the pop method in our top=most class.
Now that we have our dynamic array, we must put it to use. The easiest way to see how it works is to store some data in the array and then redisplay it.
'******** Example Code Using Dynamic Array *****
<%
Dim counter
Dim my_array
set my_array=New Dynamic_Object_Array_Class
counter=0
'**************************************************
'********* Putting Data into My Generic ******
'**************************************************
my_array.allitems()(counter).item="Hello"
counter=my_array.push 'this increments the number of items by one
my_array.allitems()(counter).item="TRUE FALSE"
my_array.allitems()(counter).putValue 0,"yes"
my_array.allitems()(counter).putValue 1,"no"
counter=my_array.push
my_array.allitems()(counter).item="Your Name"
counter=my_array.push
my_array.allitems()(counter).item="Another Item"
counter=my_array.push
my_array.allitems()(counter).item="Place in Contest"
my_array.allitems()(counter).putValue 0," first " 'automatically increase values for items
my_array.allitems()(counter).putValue 1,"second"
my_array.allitems()(counter).putValue 2,"third"
counter=my_array.push
my_array.allitems()(counter).item="Hotel Choice"
my_array.allitems()(counter).putValue 0,"Holiday Inn"
my_array.allitems()(counter).putValue 1,"Days Inn"
'**************************************************
'***** end putting data into my generic ******
'**************************************************
%>
<html>
<body>
<table>
<%
for y=0 to my_array.getsize
Response.Write "<tr><td width='100'>Item: </td><td width='10'>" & _
"</td><td width='200'>" & _
my_array.allitems()(y).item & _
"</td><td width='200'></td></tr> "
for x=0 to my_array.allitems()(y).numbervalues
if my_array.allitems()(y).getValue(x)<>"" then
Response.Write "<tr><td width='100'></td><td width='10'>" & _
"</td><td width='200'>Value:</td><td width='200'>" & _
my_array.allitems()(y).getValue(x) & "</td></tr>"
end if
next
next
%>
</table>
</body>
</html>
'********************************************
After running our sample code, we should see the following in our Web browser:
'******* Example Output *********************
Item: Hello
Item: TRUE FALSE
Value: yes
Value: no
Item: Your Name
Item: Another Item
Item: Place in Contest
Value: third
Value: second
Value: first
Item: Hotel Choice
Value: Holiday Inn
Value: Days Inn
'********* end example output ****************
Summary
Dynamic arrays of objects that contain dynamic arrays of objects allow us to create the structure we need in a simplified and reusable fashion. Our arrays also assist in the sorting, filtering, and the manipulation of data. While I have not included sample code for the sorting and filtering, once you understand how the classes work, generating the sorting and filtering methods is quite easy. The source code in this article, as well as methods for sorting and filtering a dynamic array of objects, are posted to my Web site ( http://www.donnellsmith.com/samples).
About the Author
Donnell DeLeon Smith is a graduate of the University of Houston at Clear Lake. Donnell has a Bachelor's of Science in Computer Information Systems. He currently works for Schipul Technologies (www.schipul.com) as the lead programmer, although his official title is "Binary Brainiac." Donnell also runs his own software-development company in Houston that plans on releasing an artificial intelligence (AI)
software package at the end of the year that is a component add-on to Windows 2000. The component can be used in a Web application written with ASP 3.0.
|
|
|
|
|
|
|
Other Articles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Aug 3, 2000 - Recursive Functions
|
|
|
A function that calls itself repeatedly, satisfying some condition is
called a Recursive Function. Using recursion, we split a complex problem into its single simplest case.
The recursive function only knows how to solve that simplest case. You'll see the difference
between solving a problem iteratively and recursively later.
[Read This Article] [Top]
|
|
|
|
|
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.
|
|