7 Replies Latest reply on Apr 28, 2009 11:57 AM by Gregory Lafrance

    Why people do this?

    webvalue Level 1

      Hi,

       

      I am reviewing some sample code provided by Adobe Flex 2 and use them for my own learning purpose. Here is an area I don't fully understand and I hope someone can help me.

       

      In the photoviewer sample that you can launch from Help - Flex Start Page - More installed samples >> -, the class Gallery.as:

       

              public function fill(gallery:Object):void
              {
                  this.name = gallery.id;
                  this.description = gallery.description;
                  this.selected = 0;
                 
                  for (var i:int=0; i < gallery.photo.length; i++)
                  {
                      photo = new Photo(gallery.photo[i]);
                      photos.addItem(photo);
                  }
              }

       

       

      Can you replace the sentence "photos.addItem(photo);" with "photos.addItem(gallery.photo[i]);"?

       

      In other words, why in the world should the code call new Photo(gallery.photo[i])?

       

      Thanks a bunch a lot.

        • 1. Re: Why people do this?
          ntsiii Level 3

          Please use a more desdcriptive subject for your posts.

           

          That line is calling the constructor of the Photo class to create a new instance of that class with the passed in image.

           

          The next line places a reference to that instance in an ArrayCollection.

           

          No, that is not the same.  Even if that resolves to a Photo instance, which i am not sure it does, the syntax you suggest would put a *reference* to that instance in the arrayCollection.  The given syntac cratesa new instance of the Photo class.

           

          Tracy Spratt

          • 2. Re: Why people do this?
            Gregory Lafrance Level 6

            Below is the full code for the class.  

             

            This code is creating a new instance of the Photo class, which takes an object in its constructor.

             

            The code is taking the i indexed photo data from the gallery passed into the fill method.

             

            photo = new Photo(gallery.photo[i]);

             

            You could use photos.addItem(new Photo(gallery.photo[i]));

             

             

             

            package samples.photoviewer
            {
                import mx.collections.ICollectionView;
                import mx.collections.ArrayCollection;
                import mx.collections.IViewCursor;
               
                [Bindable]
                public class Gallery
                {
                    public var name:String;
                    public var description:String;
                    public var photos:ArrayCollection;
                    public var selected:int;

                    private var photo:Photo;
                   
                    public function Gallery(gallery:Object=null)
                    {
                        photos = new ArrayCollection();
                        if (gallery != null)
                        {
                            fill(gallery);
                        }
                    }
                   
                    public function fill(gallery:Object):void
                    {
                        this.name = gallery.id;
                        this.description = gallery.description;
                        this.selected = 0;
                       
                        for (var i:int=0; i < gallery.photo.length; i++)
                        {
                            photo = new Photo(gallery.photo[i]);
                            photos.addItem(photo);
                        }
                    }
                }
            }

            • 3. Re: Why people do this?
              Ansury Level 3

              You should google some object oriented programming tutorials or articles. You may not be able to find much ActionScript specific OOP info but something like Java or C++ or another language should work fine to get a grasp of what's going on. Once you learn OOP in one language it's the same in all, but yeah this is a basic OOP concept you're asking about and it will help you immensely in the future if you understand it well.

              • 4. Re: Why people do this?
                webvalue Level 1

                First, thanks for all who replied.

                 

                Second, yes I will make the subject line more specific and meaningful.

                 

                Third, let me make my question more specific.  I understand new Photo() adds an instance of Photo class, but here I am wondering why it is needed.

                 

                I think both "photos.addItem(photo)" and "photos.addItem(gallery.photo[i])" will end up with same data content for the ArrayCollection photos, except "photos.addItem(photo)" finished with a note that each record in photos is a Photo type. Thanks for bearing with me but is this correct? Is this called typecasting?

                 

                Looks like in AS 3 people do a lot of these. What would be the benefit of doing this, in short and long term?

                 

                Thanks again for everybody's help!

                • 5. Re: Why people do this?
                  Gregory Lafrance Level 6

                  gallery.photo[i]) is just data, not a Photo object. You can't add data using addChild(). photo in this case is a Photo object.

                  • 6. Re: Why people do this?
                    webvalue Level 1

                    Greg,

                     

                    It is addItem(), not addChild().  BTW, I tested that both ways created the ArrayCollection photos virtually of the same because I used a Datagrid to display them. As I said, the only difference is that Photo type was noted if the instance was made.

                    • 7. Re: Why people do this?
                      Gregory Lafrance Level 6

                      Oh yeah, addItem(). Well in one case you are adding data object to the AC, in the other case adding a Photo object. Depends on your requiements and the value you see in storing data or a Photo object.