Henry A.
Henry A.

Reputation: 391

Why can't I inherit this variable?

I get one error with this code:

An object reference is required for the non-static field, method, or property

I'm trying to inherit the itemNo variable from the derived class to put it in to a constructor, but for some reason it's not accepting it.

Full Code:

public class InvItem
{
    protected int itemNo;
    protected string description;
    protected decimal price;

    public InvItem()
    {
    }

    public InvItem(int itemNo, string description, decimal price)
    {
        this.itemNo = itemNo;
        this.description = description;
        this.price = price;
    }

    public int ItemNo
    {
        get
        {
            return itemNo;
        }
        set
        {
            itemNo = value;
        }
    }

    public string Description
    {
        get
        {
            return description;
        }
        set
        {
            description = value;
        }
    }

    public decimal Price
    {
        get
        {
            return price;
        }
        set
        {
            price = value;
        }
    }

    public virtual string GetDisplayText(string sep)
    {
        return itemNo + sep + description + " (" + price.ToString("c") + ")";
    }
}

public class Plant : InvItem
{
    private decimal size;

    public Plant()
    {
    }

    public Plant(int itemNumber, string description, decimal price, decimal size) : base(itemNo, description, price)
    {
        this.Size = size;
    }

    public decimal Size
    {
        get
        {
            return size;
        }
        set
        {
            size = value;
        }
    }

    public override string GetDisplayText(string sep)
    {
        return base.GetDisplayText(sep) + " (" + size + ")";
    }
}

It happens in the Plant class constructor with parameters. I've tried setting it to public, private, and protected, but they all yield the same result.

Upvotes: 2

Views: 151

Answers (3)

Mike Burdick
Mike Burdick

Reputation: 838

You can't reference the inherited property in your constructor. Do this:

public Plant(int itemNumber, string description, decimal price, decimal size)
    : base(itemNumber, description, price)
{
    this.Size = size;
}

Upvotes: 0

Pranit Kothari
Pranit Kothari

Reputation: 9849

public Plant(int itemNumber, string description, decimal price, decimal size)
            : base(itemNo, description, price)
        {
            this.Size = size;
        }

While you are using it in constructor, you yet do not have access to base class members, so you are getting error.

Upvotes: 0

BradleyDotNET
BradleyDotNET

Reputation: 61379

When you are calling base on the constructor, those variables don't even exist yet (the base class hasn't been constructed).

Thus, you can't pass the class member to it in the base constructor call. Instead, you needed:

public Plant(int itemNumber, string description, decimal price, decimal size) : 
    base(itemNumber, description, price)
{
}

This will pass the parameters provided to Plant along to the base class, which is what you wanted.

Upvotes: 3

Related Questions