Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

toString Method

  • 15-11-2012 02:13PM
    #1
    Registered Users, Registered Users 2 Posts: 16


    I have this simple toString method in my code:

    public String toString()
    {return "Item ", desc, " has product code ", code, ", and the price is €", price;}

    however, I am getting an error that:

    This method must return a result of type String

    Anybody know what I've done wrong, and more importantly how to rectify it?!


Comments

  • Registered Users, Registered Users 2 Posts: 40 Marko_Polo


    Try:

    public String toString()
    {return "Item " + desc + " has product code " + code, " + and the price is €" + price;}


  • Registered Users, Registered Users 2 Posts: 16 awney


    Legend, thank you :)


  • Closed Accounts Posts: 2,930 ✭✭✭COYW


    Use string.format:
    string.Format("Item {0} has the product code {1}, and the price is {2}.", desc, code, price);


Advertisement