Solved

copying properties of an instance of a class

Posted on 2011-03-22
4
293 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

730 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