1

I am new to C# and trying to figure out how inheritance works. I'm getting the following error. Why must the parent parameters be static?

Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Rectangle.name' PurrS PATH\Sign.cs 15 Active

Parent:

namespace PurrS.Maps
{
public class Rectangle
{
    protected string name;
    protected int id;
    protected float a;
    protected float b;
    protected float c;
    protected float d;
    protected int posX;
    protected int posY;
    //A----B
    //|    | 
    //C----D

    public Rectangle(string name, int id, float a, float b, float c, float d, int posX, int posY)
    {
        this.name = name;
        this.id = id;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

}
}

Child:

namespace PurrS.Maps
{

public class Sign : Rectangle
{
    string message;

    public Sign(string message) 
        : base(name, id, a, b, c, d, posX, posY) { //This is where it fails.
        this.message = message;

    }
}
}

4 Answers 4

2

The problem you are seeing stems from the fact that Rectangle has a single constructor - the only way to create an instance of Rectangle is to pass it your 8 parameters.

When you create a Sign that inherits from Rectangle - because it is a Rectangle - it needs to be able to call its Rectangle constructor to be able to successfully construct itself.

Therefore, it needs all the parameters available to call a constructor on Rectangle (you have only one), at the time that its constructor is called.

You can ask for the parameters in Sign, or hardcode them in the Sign constructor:

 public Sign(string message, string name, int id, float a, float b, float c, float d, int posX, int posY) 
     :base(name,id,a,b,c,d,posX,poxY)

 public Sign(string message) : base("a name", 1, 1, 2, 3, 4, 10, 10)

for example.

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

Comments

2

You must either pass the parameters from a constructor with some more arguments

public Sign(string name, int id, float a, float b, float c, float d, int posX, int posY, string message)
                : base(name, id, a, b, c, d, posX, posY)
            { //This is where it fails.
                this.message = message;

            }

or provide some default fixed values:

        public Sign(string message)
            : base("foo", 1, 0, 0, 0, 0, 1, 1)
        { //This is where it fails.
            this.message = message;

        }

1 Comment

And here I thought the parameters would automagically be passed on to the parent class. Thank you.
1

You need to expand on this:

public Sign(string message) 
    : base(name, id, a, b, c, d, posX, posY) { //This is where it fails.
    this.message = message;
}

To pass the parameters to the base class as follows:

public Sign(string message, string name, int id, etc...) 
    : base(name, id, a, b, c, d, posX, posY) { 
    this.message = message;
}

Comments

0

Inheritance means that you child class (the Sign class) will have all of the fields and methods that are in the parent class. Therefore, you can say

public Sign(string name, int id, float a, float b, float c, float d, int posX, int posY)
    {
        this.name = name;
        this.id = id;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

And not have to declare any of the fields that you are using because they are inherited from the parent class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.