asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

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.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jun 6, 2002 - Client Side Validation Using the XMLHTTPRequest Object
    Jonathan Zufi shows how to use the XMLHTTP object within JavaScript or VBScript to validate form-field information without having to submit a page and wait for the result.
    [Read This Article]  [Top]
    Nov 6, 2001 - Writing Your Own Script File to Migrate a Database
    Learn how to write a script file using SQL Server's Bulk Copy Program for easy and speedy database migration.
    [Read This Article]  [Top]
    Sep 5, 2001 - Firing Events in a Shared Hosting Environment
    Firing events on a Web server is an easy task. However most of the easy solutions require you to have your own dedicated IIS or SQL Server on the Internet to play with, a privilege not shared by many. In this article, Matthew Muller shows you how to get the same functionality in a shared hosting environment.
    [Read This Article]  [Top]
    Jun 26, 2001 - Dynamically Changing Static Web Galleries
    Web galleries are an easy way to add interactivity and content to your Web site. However, how do you keep the Web galleries consistent with your site and how do you overcome the deficiencies of your Web gallery creation tool? John Sorensen explains a simple way to do both.
    [Read This Article]  [Top]
    Mar 27, 2001 - Using ASP to Send a Wireless Text Message
    Even though SMS is now in high gear, developers remain slated with restrictive limits to carrier resources. Sending an SMS message via e-mail requires the acceptance of several hidden flaws. Joe Lauer shows how to avoid these complications by sending a wireless text-message through the use of ASP.
    [Read This Article]  [Top]
    Mar 1, 2001 - Server-Side Validations Using Regular Expressions
    Add punch to your validation routines by adding regular expressions. Further prepare yourself for the coming ASP.NET regular expression validation control. This article shows you how to use regular expressions and provides sample patterns for different user inputs.
    [Read This Article]  [Top]
    Sep 20, 2000 - How to Display File ACLs on Your Web Page without Active Directory
    Thought displaying file ACLs on a Web page in a browser was impossible without Active Directory installed? Think again. Through a patchwork of technologies, Larry Schwartz proves otherwise.
    [Read This Article]  [Top]
    Aug 11, 2000 - Servers-Side Validations on the Client Side
    Servers-side validations on the client side...isn't that an oxymoron? Maybe, but Pandurang Nayak shows us how to accomplish a type of remote scripting using a mix of Javascript and ASP.
    [Read This Article]  [Top]
    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]
    Jul 27, 2000 - Effect of Using Multiple Scripting Languages in ASP
    Do you know what happens when you use multiple languages within your ASP page? Gopikrishna S throws light on how an ASP page behaves when multiple languages are used for server side scripting.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry