Solved

Simple question: hows to dynamically reference a class object

Posted on 2011-03-16
7
329 Views
Last Modified: 2012-08-14
Hi i  need a little help as my brain has gone dark on me :-)

i have the below object backupCartItem (simplified)

and i have a function which needs to reference different properties within the different contained objects.

public function createEndorcementItem( propertyClass:String, propertyName:String ):void
{
var val:* = backupCartItem[ propertyClass ][ propertyName ];
trace( val.toString();
}

public function doSomething():void
{
createEndorcementItem( 'sessionVO.primaryInsurie', 'AGE_NOW');
}


the above little script example should set val to being
backupCartItem.sessionVO.primaryInsurie.AGE_NOW

but it throws an error

Property sessionVO.primaryInsurie not found on com.travins.cairngorm.model.CartItemVO

how can i dynamically reference the object please






      backupCartItem      com.travins.cairngorm.model.CartItemVO (@70bd781)      
            admin_fee      0      
            id      1174 [0x496]      
            sessionVO      com.travins.cairngorm.model.CurrentSessionVO (@27a2881)      
                  ADULT_COUNT      1      
                  CHILD_COUNT      0      
                  primaryInsurie      com.travins.cairngorm.model.InsurieVO (@1fa5dc1)      
                        AGE_AT_START_TIME      40 [0x28]      
                        AGE_ERROR      ""      
                        AGE_IS_VALID      true      
                        AGE_NOW      40 [0x28]      
                        COUNTRY_ID      1      
                        DATE_OF_BIRTH      Date (@71dbcd1)      
0
Comment
Question by:cghrmauritius
  • 5
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 35149658
Hi,
You mixed in your code setter and getter that reason why you confused yourself.
Check if this what you want:

private var backupCartItem:Object;

			public function createEndorcementItem( propertyClass:String, propertyName:String, value:*):void
			{
				if(backupCartItem == null)
					backupCartItem = {};
				if(backupCartItem[propertyClass] == undefined)
					backupCartItem[propertyClass] = {};
				backupCartItem[propertyClass][propertyName] = value;
			}
			
			public function getEndorcementItem( propertyClass:String, propertyName:String ):void
			{
				var val:* = backupCartItem[propertyClass][propertyName];
				trace( val.toString());
			}
			
			public function doSomething():void
			{
				createEndorcementItem( 'sessionVO.primaryInsurie', 'AGE_NOW', 25);
				getEndorcementItem( 'sessionVO.primaryInsurie', 'AGE_NOW');
			}

Open in new window

0
 

Author Comment

by:cghrmauritius
ID: 35149839
humm no sorry i needed it the other way round im not trying to set the value im trying to get the value of

backupCartItem[ propertyClass ][ propertyName ]

i know there is a value cause if i query it manually as

backupCartItem.sessionVO.primaryInsurie.AGE_NOW  i do ge the cirrect value but i cant get it when using the square brackets

any ideas please
0
 

Author Comment

by:cghrmauritius
ID: 35150016
i see yuor example does work, i must have something wrong with my backupcartItem but i dont understand why because i can get the value if done manually

thanks i will assign you the points

thanks for your time
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Closing Comment

by:cghrmauritius
ID: 35150021
Thanks
0
 

Author Comment

by:cghrmauritius
ID: 35150077
Actually if checked in debuger your example isnt right im afraid

backupCartItem      Object (@8cb9421)      
      sessionVO.primaryInsurie      Object (@8cb93f9)      
            AGE_NOW      25 [0x19]      


as per my original which is what my object dump looks like

backupCartItem      com.travins.cairngorm.model.CartItemVO (@70bd781)      
             sessionVO      com.travins.cairngorm.model.CurrentSessionVO (@27a2881)      
                   primaryInsurie      com.travins.cairngorm.model.InsurieVO (@1fa5dc1)      
                        AGE_IS_VALID      true      
                        AGE_NOW      40 [0x28]      

please notice that in your example the primaryInsurie object isnt nested inside sessionVO

0
 

Author Comment

by:cghrmauritius
ID: 35150262
ok i have found a solution finally your comment gave me an idea which worked so thanks for the pointers


here is my solution but im sure its a dirty method and that there must be a cleaner way!!!

public function createEndorcementItem( propertyClass:String, propertyName:String ):void
{
	var arr:Array = propertyClass.split(".");
				
	var cur:Object;
	cur = backupCartItem;
				
	for each (var a:String in arr)
	{
		cur = cur[a];
	}
	var val:* = cur[propertyName];
}

Open in new window

0
 
LVL 29

Expert Comment

by:dgofman
ID: 35150302
I am very happy to know I helped you.
Regards,
David
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now