0

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?

3
  • 1
    If you don't have a parameter name in the constructor of ItemToBuy, you cannot call the constructor of the base class that requires a name parameter. If you don't have it, then add a constructor to the base class that takes no parameters or change your ItemToBuy constructor to require a name parameter to pass down to the base class Commented Oct 4, 2017 at 8:43
  • 1
    OK, so think about this for a minute. The base class needs a name. Therefore any derived class is going to need to pass a name to the base class constructor. It can't just conjure this up out of thin air - either the derived class creates a name somehow and passes it to the base class' constructor, or name must be an argument of the derived class' constructor and then passed through to the base class' constructor. Commented Oct 4, 2017 at 8:44
  • 1
    Possible duplicate of C# "An object reference is required for the non-static field, method, or property" Commented Oct 4, 2017 at 8:51

4 Answers 4

4

your ItemToBuy class does not have any knowledge of "name". The way you build the constructor, "name" needs to be a defined string.

Let's say that your constructor looks like this:

class ItemToBuy : Item
{
    private int lowPrice;
    private int highPrice;
    private int maxQuantity;

    public ItemToBuy(int lowPrice, int highPrice, int maxQuantity, string name) : base(name)
    {
        this.lowPrice = lowPrice;
        this.highPrice = highPrice;
        this.maxQuantity = maxQuantity;
    }
}

this will work because the name parameter is defined.

So, you either do it like that or pass a hardcoded value like you did to make it work.

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted because this is the only answer that actually explains the problem.
2

You need to have the name in the ctor of the ItemToBuy class too

public ItemToBuy(string name ,int lowPrice, int highPrice, int maxQuantity) : base(name)

Comments

0
public ItemToBuy(int lowPrice, int highPrice, int maxQuantity) : base(name)
{
    this.lowPrice = lowPrice;
    this.highPrice = highPrice;
    this.maxQuantity = maxQuantity;
}

should be changed to:

public ItemToBuy(int lowPrice, int highPrice, int maxQuantity, string name) : base(name)
{
    this.lowPrice = lowPrice;
    this.highPrice = highPrice;
    this.maxQuantity = maxQuantity;
}

You need to specify the name parameter in the constructor, as per my code above.

2 Comments

Stylistically, I would put the name parameter first in the ItemToBuy constructor.
It would depend on context as to whether that makes sense, but yep @Polyfun that is certainly valid. name can be at any position (not necessarily the first or last).
0

you need to receive the name in the constructor of ItemToBuy:

public ItemToBuy(int lowPrice, int highPrice, int maxQuantity,string name) : base(name)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.