Adding a setter to a subclass inheriting from an abstract class

J

jehugaleahsa

Hello:

I'm implementing a Table in GDI+. I represent ColumnHeaders and Cells
with the same abstract class ACell. The only difference between Cell
and ColumnHeader is how widths are specified. A ColumnHeader is
responsible for indicating the width of the column. All cells look to
their respective ColumnHeader to get their width.

I would like to provide the ColumnHeader with a setter for Width,
while restricting it for the Cell. I would hate to provide a no-op
setter for the Cell's Width.

I know you can add a setter if implementing an interface. This isn't
true for abstract classes . . . I'm not sure why.

Is there a language feature to deal with this? How do I deal with it?

Thanks,
Travis
 
M

Marc Gravell

It sounds like "new" may be your best option (below).

Marc

class Foo
{
private int width;
public int Width
{
get {return width;}
}
protected void SetWidth(int width)
{
if (width < 0) throw new ArgumentOutOfRangeException("width");
this.width = width;
}
}
class Bar : Foo
{
new public int Width
{
get { return base.Width; }
set { SetWidth(value); }
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top