Solved

copying properties of an instance of a class

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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,…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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 select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

705 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

19 Experts available now in Live!

Get 1:1 Help Now