0

Hi my problem is i have to be able to reference certain fields inside my Customer object.]

I am studying AS3 at the moment and being taught custom classes, but we are taught to use the toString method of returning a value i guess you could call it, what i need is to be able to call one field to identify the object i.e. name field from the object in the array, here's my code

package valueObjects
{
public class Person
{
    //instance variables
    protected var name:String;
    protected var address:String;
    protected var phoneNo:String;   

    public function Person(n:String,a:String,p:String)
    {
        name=n;
        address=a;
        phoneNo=p;
    }

    public function toString():String
    {
        //returns string 
        return name+":"+address+":"+phoneNo;
    }
}

}

some reason it will not put that whole block of code together like THIS IS

So now how do i define it not toString but in object form ??

1
  • If you're trying to access the name, address and phoneNo vars from a different class, then you need to define them as public, not protected Commented Jun 10, 2013 at 2:57

1 Answer 1

1

I think what you are trying to do is access the name, address and phoneNo vars from a different class?

If so, you have to declare them as public vars instead of private vars.

public var name:String; //now this can be accessed from other classes:  thisClassInstance.name

If you want to have them read-only from other classes, you have to use a getter method:

protected var name_:String;  //local var name for full access;
public function get name():String {
    return name_; //this can be access by doing  thisClassInstance.name
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Steve Andrews this explains the problem you are having in this question. Your Customer class only has a toString() method, and no way of accessing the individual properties of name, address, and phoneNo. If you add getter methods (as shown here) for each of these properties, then you should be able to do <s:ComboBox labelField="name" /> as well as access these properties individually (ie: customer.phone)
@LondonDrug thank you for you help, i've got it displaying the name field only
& @Sunil thank you for you help too in this and the other post of mine, i've got it displaying the name field only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.