-1

This is my Program.cs:

int currentHp = 100;
Potion potion = new Potion(10);
Console.WriteLine(potion.ToString(currentHp));

This is my class called Potion.cs

private int hpHeal;

public Potion(int hpHeal)
{
    HpHeal = hpHeal;
}
public int HpHeal
{
    get { return hpHeal; }
    set
    {
        hpHeal = value;
    }
}

public override string ToString(int currentHp)
        {
            if (currentHp + hpHeal > 100)
            {
                string result = "";
                result += $"You healed for {100 - currentHp} hp.\n";
                currentHp = 100;
                result += $"Current health: {currentHp} hp.";
                return result;
            }
            else
            {
                string result = "";
                currentHp += hpHeal;
                result += $"You healed for{hpHeal} hp.\n";
                result += $"Current health: {currentHp} hp.";
                return result;
            }
        }

The line public override string ToString(int currentHp) is underlined with an error 'Potion.ToString(int)': no suitable method found to override and I don't know why... I'm currently learning how Classes work so don't judje me too hard.

Alice
  • 3
  • 1
  • 4
    To override a method, a method in the base class has to exist and be `abstract` or `virtual`. If you look at the base class, `object`'s [ToString method](https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-7.0), you'll see that its signature is `public virtual string ToString ()`, not `public virtual string ToString(int currentHp)`. – ProgrammingLlama Nov 26 '22 at 13:06
  • You can add one or more ToString() versions to your class, besides the one you're overriding, as `public virtual string ToString(int somevalue) { return "something " + somevalue }`. Or non `virtual`, if it's not meant to be overridden (and probably seal the class) – Jimi Nov 26 '22 at 15:36

2 Answers2

2

ToString is a "predefined" method that cannot take any arguments. In your case, simply rename your method (e.g., to Stringify) and remove the override keyword from its definition.

Jan Joneš
  • 777
  • 6
  • 13
  • 1
    Stringify is not a good name. Using ToString was also not good design. The method is doing to much stuff. Stringifying is just a small part of it. This probably deserves an own class or helper – JHBonarius Nov 26 '22 at 13:34
0

You’re trying to override a method which doesn’t exists at all in the base class.

You need to rename your ToString method to some meaningful name as per your business logic like GetCustomString

One suggestion, you should also rename int hpHeal; because you have same parameter in the constructor, it’s confusing. Better name it something like _hpHeal.

And your can also reduce the number of string assignments here, few are unnecessarily affecting performance as string are immutable and it creates a new string when you assign it again.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • There is no need to change ```int hpHeal``` to something else, cause the constructor sends the value it receives to the ```private int hpHeal``` if I express myself correctly. So basically they are the same thing. That's how they teach us at school to make them the same name. – Alice Nov 26 '22 at 17:17
  • @Alice https://stackoverflow.com/q/268587/6527049 – Vivek Nuna Nov 26 '22 at 17:30