Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

copying properties of an instance of a class

Posted on 2011-03-22
4
Medium Priority
?
310 Views
Last Modified: 2012-05-11
Hi , I wonder if someone could help please, Im learning slowly but am still very green

I am iterating through an array which contains instances of various Classes that each extend from a custom class  PageItemClass.

I need to have a  function that when a certain condition is met, (the "if (headerContent[0][0].... " line)

1) the type of class that is the current item is established
2) a new instance of the class in question is created
3) the values of the properties of the initial instance are copied to the new instance.

I think I have managed the first two (lines 7 and 8 , but the third is stumping me, I have tried ObjectUtil.Copy(item) which indeed works but returns me an object, how do I get a copy which both has the same property values and is of the same ClassType,
 i have also tried the clone method of ObjectUtil, but that just complains about not having a uid.
thanks in advance

John
if (pageData.headerTypeData){
   for (var i:int=0; i< pageItems.length; i++){
     for (var j:int =0; j<pageItems[i].length; j++ ){
         var item:PageItemData=pageItems[i][j];
						
	if (headerContent[0][0]!="" && headerContent[0][0]==item.type){
	var classType:Class =getClass(item);
	var newItem=new classType;
						
	}	
    }
}
private static function getClass(obj:Object):Class {
	return Class(getDefinitionByName(getQualifiedClassName(obj)))
	}

Open in new window

0
Comment
Question by:BloodGrinder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 35192966
Try bytes clone

            var buffer:ByteArray = new ByteArray();
            buffer.writeObject(source);
            buffer.position = 0;
            return buffer.readObject();
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35193007
For example:

public static function clone(source:*):Object{
            var buffer:ByteArray = new ByteArray();
            buffer.writeObject(source);
            buffer.position = 0;
            return buffer.readObject();
}

var clone:Object = clone(item);
var classType:Class = getClass(item);
var o:PublisherAccount = new classType();
      for(var name:String in clone){
            var value:Object = clone[name];
            o[name] = value;
      }
}
0
 
LVL 29

Accepted Solution

by:
dgofman earned 2000 total points
ID: 35193093
Sorry you need to use the diffrent names:

var cloneItem:Object = clone(item);
var classType:Class = Class(getDefinitionByName(getQualifiedClassName(item)))
var newItem:* = new classType();
for(var name:String in cloneItem){
    var value:Object = cloneItem[name];
    newItem[name] = value;
}
0
 

Author Closing Comment

by:BloodGrinder
ID: 35194093
Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

610 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