Javawag

OK, so I'm making a very simple game engine and there are two objects so far: engineObject and objRedBall, like this:

Code Snippet

public class engineObject

{

public int x = 0, y = 0, width = 0, height = 0, image = 0;

public float hspeed = 0F, vspeed = 0F, gravity = 0F;

public Image[] sprite = new Image[] { Image.FromFile

(Application.StartupPath + @"\redBall.bmp") };

public frmTest parent;

public void physics() {...}

virtual public void step() {...}

virtual public void draw(Graphics g) {...}

}

public class objRedBall : engineObject

{

public int width = 32;

public int height = 32;

public float gravity = 0.1F;

public Image[] sprite = new Image[] { Image.FromFile

(Application.StartupPath + @"\redBall.bmp") };

override public void step() {...}

}

As you can see, the step() function is declared virtual in the engineObject class and override in the objRedBall class, which works fine. How can I make it so that the width, height, gravity, etc variables from objRedBall override those from engineObject in the same kind of way

Thanks in advance!

- Javawag



Re: Visual C# Language "Virtual and override" equivalent for variables.

Peter Ritchie

You can't make fields virtual, you can only make properties and methods virtual. You could refactor width,height,gravit, etc into properties and go from there. But, each class will have to implement the backing store for those properties itself, if it overrides them.




Re: Visual C# Language "Virtual and override" equivalent for variables.

Javawag

That I could try actually.. but isn't there an easier way

Thanks,

- Javawag





Re: Visual C# Language "Virtual and override" equivalent for variables.

Peter Ritchie

I don't see how it could be much easier:

Code Snippet

public class Entity

{

private Size size;

public virtual Size Size

{

get { return size; }

set { size = value; }

}

}

public class Ship : Entity

{

private Size shipSize;

public override Size Size

{

get

{

return shipSize;

}

set

{

shipSize = value;

}

}

}






Re: Visual C# Language "Virtual and override" equivalent for variables.

Javawag

Yeah, it is really easy like that I just meant it's long winded. Nothing #region & #endregion directives can't fix though! Thanks!

- Javawag





Re: Visual C# Language "Virtual and override" equivalent for variables.

George Olson

Maybe another option is to set the values in the constuctor. Peter is a C# genius so maybe he knows the pros and cons of doing this.

Code Snippet

public class engineObject

{

public engineObject()

{

x = 0; y = 0; width = 0; height = 0; image = 0;

hspeed = 0F; vspeed = 0F; gravity = 0F;

}

public int x, y, width, height, image;

public float hspeed, vspeed, gravity;

protected Image[] sprite = new Image[] { Image.FromFile

(Application.StartupPath + @"\redBall.bmp") };

public frmTest parent;

public void physics() {...}

virtual public void step() {...}

virtual public void draw(Graphics g) {...}

}

public class objRedBall : engineObject

{

public objRedBall():base()

{

width=32;

height=32;

gravity=0.1F;

}

override public void step() {...}

}