2 Replies Latest reply on Jul 11, 2010 7:48 AM by John Hall

    Casting/converting generic object to custom class

    John Hall Level 4

      I know the right way to do what I want is to use ORM and type all my objects in the database I have, but it's a bit late for that approach.

       

      So, I have an arrayCollection that is returned from ColdFusion. I have a custom class, let's call it MyClass. What's the best way, assuming the properties match 1-1, to copy all properties of the generic object within the arrayCollection to an instance of myClass with the same properties. I've tried "as MyClass" and no luck (that would be nice. I have get to hit on a syntax to use "for each" so if you have an example or other suggestions, I'd appreciate it.

       

      Right now, I'm adding all the parameters to the contructor of MyClass if there's an object passed in, it copies the properties to a new instance, but that feels 'dirty' in the sense of having to hard code that mapping:

       

              public var GUID:String;
              public var frameName:String;
              public var frameType:String;
              public var helpTitle:String;
              public var helpText:String;
              public var baseMovieGUID:String;
              public var notes:String;
              public var hotspots:ArrayCollection;
              public var JSONHotspots:String;
              public var baseMovieURL:String;

       

              public function MyClass(obj:Object = null):void {
                  if (obj != null){
                      GUID = obj.GUID;
                      frameName = obj.frameName
                      frameType = obj.frameType
                      helpTitle = obj.helpTitle
                      helpText = obj.helpText
                      baseMovieGUID = obj.baseMovieGUID
                      notes = obj.notes
                      hotspots = obj.hotspots
                      JSONHotspots = obj.JSONHotspots
                      baseMovieURL = obj.baseMovieURL
                  }
              }
          }