cghrmauritius
asked on
Simple question: hows to dynamically reference a class object
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.p rimaryInsu rie.AGE_NO W
but it throws an error
Property sessionVO.primaryInsurie not found on com.travins.cairngorm.mode l.CartItem VO
how can i dynamically reference the object please
backupCartItem com.travins.cairngorm.mode l.CartItem VO (@70bd781)
admin_fee 0
id 1174 [0x496]
sessionVO com.travins.cairngorm.mode l.CurrentS essionVO (@27a2881)
ADULT_COUNT 1
CHILD_COUNT 0
primaryInsurie com.travins.cairngorm.mode l.InsurieV O (@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)
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'
}
the above little script example should set val to being
backupCartItem.sessionVO.p
but it throws an error
Property sessionVO.primaryInsurie not found on com.travins.cairngorm.mode
how can i dynamically reference the object please
backupCartItem com.travins.cairngorm.mode
admin_fee 0
id 1174 [0x496]
sessionVO com.travins.cairngorm.mode
ADULT_COUNT 1
CHILD_COUNT 0
primaryInsurie com.travins.cairngorm.mode
AGE_AT_START_TIME 40 [0x28]
AGE_ERROR ""
AGE_IS_VALID true
AGE_NOW 40 [0x28]
COUNTRY_ID 1
DATE_OF_BIRTH Date (@71dbcd1)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
thanks i will assign you the points
thanks for your time
ASKER
Thanks
ASKER
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.mode l.CartItem VO (@70bd781)
sessionVO com.travins.cairngorm.mode l.CurrentS essionVO (@27a2881)
primaryInsurie com.travins.cairngorm.mode l.InsurieV O (@1fa5dc1)
AGE_IS_VALID true
AGE_NOW 40 [0x28]
please notice that in your example the primaryInsurie object isnt nested inside sessionVO
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.mode
sessionVO com.travins.cairngorm.mode
primaryInsurie com.travins.cairngorm.mode
AGE_IS_VALID true
AGE_NOW 40 [0x28]
please notice that in your example the primaryInsurie object isnt nested inside sessionVO
ASKER
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!!!
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];
}
I am very happy to know I helped you.
Regards,
David
Regards,
David
ASKER
backupCartItem[ propertyClass ][ propertyName ]
i know there is a value cause if i query it manually as
backupCartItem.sessionVO.p
any ideas please