简体   繁体   中英

How to fix override ToString method

I am setting up a class with a GUI and having issues on this mandatory ToString method I need in my code. I missed 2 classes due to traveling for a family emergency and now just a little lost on what exactly I am doing here.

Honestly, I don't understand much of whats going on, so I am looking for an explanation. But I have tried watching videos and moving around the code to no avail.

class Sandwich
{
    public string name = "Tony";
    public string meat = "None";
    public int tomatoSlices = 1;

    public override tomatoSlices.ToString()
        {
       public double ComputerPrice()
        {
              return 4.0 + (0.5 * tomatoSlices);
        }
    }
}

The program should run, but not sure why it isnt. It has something to do with the tomatoSlices integer, I suppose.

As mentioned in the comments you have declared a method inside another method. You need to move the ComputerPrice() outside of the ToString method. Also, you need to remove the tomatoSlices from the ToString definition:

class Sandwich
{
    public string name = "Tony";
    public string meat = "None";
    public int tomatoSlices = 1;

    public double ComputerPrice()
    {
         return 4.0 + (0.5 * tomatoSlices);
    }

    public override string ToString()
    {
        return ComputerPrice().ToString();
    }
}

Now, when you call sandwich.ToString() it will return the value of ComputerPrices() as a string eg:

var sandwich = new Sandwich();

var price = sandwich.ToString();

It's a little hard to tell exactly what you want, but you have a method nested inside another method, which is not legal.

Probably you want to move ComputerPrice() out of the ToString() method, and then implement what you want for ToString() (which is typically the string representation of the class).

Also, you don't specify tomatoSlices as part of the method name; when you override a base class method, you just use the base class method name, which is ToString() . You also have to declare a return type of string for the method.

Other things you may want to do are:

  1. Use public properties instead of fields
  2. Use PascalCase for public members
  3. Use the decimal datatype for working with currency (to avoid rounding errors)

Here's a sample class that addresses all these issues:

class Sandwich
{
    public string Name { get; set; }
    public string Meat { get; set; }
    public int TomatoSlices { get; set; }

    public Sandwich()
    {
        Name = "Tony";
        Meat = "None";
        TomatoSlices = 1;
    }

    public override string ToString()
    {
        return $"This sandwich named {Name} " +
               $"has {Meat} meat and {TomatoSlices} slices of " +
               $"tomato, for a total cost of {ComputerPrice()}";

         // Or just return the price if that's what you want instead:
         // return ComputerPrice().ToString();
    }

    public decimal ComputerPrice()
    {
        return 4M + 0.5M * TomatoSlices;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM