3 Replies Latest reply on Jun 13, 2009 5:21 PM by GordonSmith

    Passing a value to a class instance

    SiHoop Level 1

      I'm trying to pass a unique value to an instance of a class, but end up sending the same value to every instance. The class creates a button. When the button is pressed I want to retrieve the value sent to the instance. myVar is a public variable declared in the class. I pass the value to the class using:


      public    var myClass:SomeClass=new SomeClass();




      myClass.myVar =counter


      When I press a button that exists in the class I want to be able to retrieve myVar, but the value that comes out is always equal to the value that was generated for the last instance. So if I generate 5 instances, the value of myVar is 5 for each button that I press. How do I make the unique value of myVar stay with the instance?

        • 1. Re: Passing a value to a class instance
          leybniz Level 4

          Whole code please

          • 2. Re: Passing a value to a class instance
            SiHoop Level 1

            I tried to simplify my code to send you an example, but ended up making the problem even worse. Let me state the problem differently:

            Suppose you had a class that created boxes. When a box was clicked it would display a unique number that had been assigned to it when it was created. How would I do this?

            I assume I would declare a public property inside the class. Then, when the class was instantiated, I would pass the instance a value. This makes sense to me, but seems to be wrong. Instead, what I get is each ball getting the same value, the last one created.

            • 3. Re: Passing a value to a class instance
              GordonSmith Level 4

              If you write a class with an instance var such as


              public class MyBox


                  public var value:int;



              then in a method of another class you can write


              var myBox:MyBox = new MyBox();

              myBox.value = 17;


              and the value 17 will be stored in that one instance of MyBox.


              You could also make it possible to set the box's value in the constructor:


              public class MyBox


                  public function MyBox(value:int)



                      this.value = value;



                  public var value:int



              var myBox:MyBox = new MyBox(17);


              Gordon Smith

              Adobe Flex SDK Team