1

I've Google'd "calling base constructor", and I'm not getting the answers I need.

Here's the constructor I have;

public class defaultObject
{
    Vector2 position;
    float rotation;
    Texture2D texture;

    public defaultObject(Vector2 nPos, float nRotation, Texture2D nTexture)
    {
        position = nPos;
        rotation = nRotation;
        texture = nTexture;
    }
}

Now I have that in place, I want to inherit the constructor and all its workings. This is what I'd expect to do;

public class Block : defaultObject
{
    // variables inherited from defaultObject
    public Block : defaultObject; //calls defaultObject constructor
}

Why can't I do that?

0

4 Answers 4

6

Use : base():

public class Block : defaultObject
{
    // variables inherited from defaultObject
    public Block ()
        : base()
    {}
}

or with parameters:

public class Block : defaultObject
{
    // variables inherited from defaultObject
    public Block (Vector2 nPos, float nRotation, Texture2D nTexture)
        : base(nPos, nRotation, nTexture)
    {}
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried using : base(), now it's complaining that ':' is an invalid token.
I missed the () on the parameterless constructor, try adding those.
1

Why is it hiding an inherited member?

Because I bet the method in the base class is not marked virtual.


I see that you removed that part of the question. Well, I've answered it anyway now...

1 Comment

Just fixed that, merci.
0

To call the base class's constructor:

public class Block : defaultObject
{
    // variables inherited from defaultObject
    public Block(npos, rotation, texture) : base(npos, rotation, texture); //calls defaultObject constructor
}

To override the update method, you must declare it virtual in the base class:

public virtual void update() { .. }

2 Comments

You're missing the types for the parameters to Block(...).
Isn't there a quicker way? I end up writing the same constructor all over again even though I don't want to change it.
0

you have multiple problems in your code. It should be like this

public Block(Vector2 nPos, float nRotation, Texture2D nTexture) : base(nPos,nRotation,nTexture) // see the way params are passed to the base constructor
{}

5 Comments

Are you all just saying "param1" just to be quick?
yes.I have updated my answer to remove the confusion.
In your solution, I will have to end up typing all the constructor values when the base constructor does that already. Isn't there simpler way to refer the program to the base constructor?
you can call a parameter less constructor as well
Thank you. I've tried this and it seems happy, but now the object isn't being drawn in XNA.

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.