I'm trying to inherit from a base class but I'm getting an error that I can't figure out. This is the base class:
class Item
{
protected string name;
public Item(string name)
{
this.name = name;
}
}
And this is the inherited class:
class ItemToBuy : Item
{
private int lowPrice;
private int highPrice;
private int maxQuantity;
public ItemToBuy(int lowPrice, int highPrice, int maxQuantity) : base(name)
{
this.lowPrice = lowPrice;
this.highPrice = highPrice;
this.maxQuantity = maxQuantity;
}
}
The issue is this line:
public ItemToBuy(int lowPrice, int highPrice, int maxQuantity) : base(name)
where 'name' is underlined with the error message "An object reference is required for the non-static field, method or property 'Item.name'. If I replace it with a string literal the error message isn't there. What am I doing wrong with inheriting the constructor?
name. Therefore any derived class is going to need to pass anameto the base class constructor. It can't just conjure this up out of thin air - either the derived class creates anamesomehow and passes it to the base class' constructor, ornamemust be an argument of the derived class' constructor and then passed through to the base class' constructor.