Solved

copying properties of an instance of a class

Posted on 2011-03-22
4
294 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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.

695 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