#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Arrays of Component Objects


    Before I give detailed examples, can anyone provide any insight into tips or tricks on creating arrays of objects? I'm currently attempting to create these arrays and it is giving me a bit of a problem.

    I am attempting to create a main User structure that holds multiple variables including an array of account objects. Each account object has parameters as well as an array of sub-objects.

    I am able to create the first array of objects, but when I refer to a function within one of those objects to create a sub-object, my code yells at me.

    I can provide details later, but looking for any inside tips or tricks I may be forgetting first.

    Thanks
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0
    After looking at this a little longer, I realize this may not make any sense without an example. Here is the code pages that I have:

    User.cfc
    Code:
    <cfcomponent displayname="User" hint="Defines a user" output="false">
    
    	<cffunction name="Init" output="false" access="public">
    		<cfargument name="username" type="string" required="false" default="myuser" />
    		
    		<cfset variables.username = Trim(arguments.username) />
    		<cfset variables.accounts = ArrayNew(1) />
    		
    		<cfreturn this />
    	</cffunction>
    	
    	<cffunction name="addAccount" output="false" access="public" returntype="void">
    		<cfargument name="accountNumber" type="numeric" required="true" />
    		
    		<cfset variables.accounts[ArrayLen(variables.accounts)+1] = createObject("component","/LearningArea/smahony/ObjectTesting/account").init() />
    		
    		<cfset variables.accounts[ArrayLen(variables.accounts)].addAddress(1234567890) />
    		
    		<cfreturn />
    	</cffunction>
    	
    </cfcomponent>
    Account.cfc
    Code:
    <cfcomponent displayname="Account" output="false">
    	
    	<cffunction name="init" access="public" returntype="void">
    		<cfargument name="accountNumber" type="numeric" required="false" default="0" />
    				
    		<cfset variables.accountNumber = arguments.accountNumber />
    		<cfset variables.addresses = arrayNew(1) />
    		
    		<cfreturn />
    	</cffunction>
    	
    	<cffunction name="addAddress" output="false" access="public" returntype="void">
    		<cfargument name="premiseID" type="numeric" required="true" />
    		
    		<cfset variables.addresses[ArrayLen(variables.addresses)] = createObject("component","address").init() />
    		
    		<cfreturn />
    	</cffunction>
    	
    </cfcomponent>
    Address.cfc
    Code:
    <cfcomponent displayname="Address" output="false">
    
    	<cffunction name="Init" output="false" access="public">
    		<cfargument name="addressID" type="numeric" required="false" default="0" />
    		
    		<!--- Initialize all variables needed --->
    		<cfset variables.addressID = arguments.addressID />
    		
    		<cfreturn this />
    	</cffunction>
    
    </cfcomponent>
    What is the best way to do this? When I try to add a new address object right after creating the account object it yells saying element 1 cannot be found. I'm at a loss (or not understanding what I'm trying to do)
  4. #3
  5. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    You might be able to avoid this sort of problem if you just use ArrayAppend() to add items to the arrays.

IMN logo majestic logo threadwatch logo seochat tools logo