?
Solved

copying properties of an instance of a class

Posted on 2011-03-22
4
Medium Priority
?
303 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
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): …
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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