Overriding static field

12,135

Solution 1

Statics members aren't called polymorphically - it's as simple as that. Note that you talk about the property staying static - you don't have a static property at the moment. You have an instance property, and two static fields.

A couple of options:

  • A virtual member which is overridden in each class, even though it doesn't need any state from the object other than its type
  • Pass the rectangle to draw into the constructor of the block, and store it in a field

Solution 2

You don't have to override drawRectangle in each class if you use the virtual keyword.

Block.cs

protected static Rectangle m_drawRectangle = new Rectangle(0, 0, 32, 32);
public virtual Rectangle drawRectangle
{
    get { return m_drawRectangle; }
}

BlockX.cs

private static Rectangle m_drawRectangleX = new Rectangle(32, 0, 32, 32);
public override Rectangle drawRectangle
{
    get { return m_drawRectangleX; }
}

Solution 3

You can't override a static member.

I realize you do not want to override drawRectangle, but that appears to be the simplest solution to implement given what you have in your question. Your best solution is to declare a new static field in each derived class and override the instance property to achieve the results you want:

public class Block
{
    private static Rectangle m_drawRectangle = new Rectangle(0, 0, 32, 32);
    public virtual Rectangle drawRectangle
    {
        get { return m_drawRectangle; }
    }
}

public class BlockX : Block 
{
    private static Rectangle m_drawRectangle = new Rectangle(0, 0, 32, 32);
    public override Rectangle drawRectangle
    {
        get { return m_drawRectangle; }
    }
}
Share:
12,135
Ashley Davies
Author by

Ashley Davies

I develop some websites and games as a hobby.

Updated on June 04, 2022

Comments

  • Ashley Davies
    Ashley Davies almost 2 years

    I'm writing a C# game engine for my game, and I've hit a problem.

    I need to do a XNA.Rectangle drawRectangle for each different type of block.

    Blocks are stored in a list of blocks, so the property, to be accessible by draw without casting a lot, must be overriden.

    I've tried lots of ways of doing it, but none work.

    Here's the current one I'm doing:

    Block.cs

    protected static Rectangle m_drawRectangle = new Rectangle(0, 0, 32, 32);
    public Rectangle drawRectangle
    {
        get { return m_drawRectangle; }
    }
    

    BlockX.cs

    protected static Rectangle m_drawRectangle = new Rectangle(32, 0, 32, 32);
    

    However when creating BlockX and accessing drawRectangle, it still returns 0, 0, 32, 32.

    Ideally I could just override the drawRectangle member, however doing this would mean creating a member in every single block class. I only want to adjust m_drawRectangle.

    Each block will be created hundreds of times so I don't want it to be non-static, and it would be silly to do it in the constructor.

    Is there any better way other than just putting a static function to initialise static things in every block?

    Edit:

    So to sum up, my requirements are:

    • Minimal extra code in BlockX.cs to override
    • Field must stay static
    • Preferably not have to override drawRectangle, only m_drawRectangle.
    • Not having to create a new rectangle every time the property is accessed