C# override syntax

  • Thread starter Thread starter Peder Y
  • Start date Start date
P

Peder Y

I wonder if it's possible to do something like this:

public class someClass {

Panel pnl;

public someClass() {
pnl = new Panel();
}


public override pnl.OnPaintBackground(...) {
}

}
 
Hi,

Your class should be derived from Panel:

public class someClass :Panel
{
public someClass ()
{ }

protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground (pevent);
}
}

Best regards,
Peter Jausovec
(http://blog.jausovec.net)
 
Peder Y said:
I wonder if it's possible to do something like this:

public class someClass {

Panel pnl;

public someClass() {
pnl = new Panel();
}


public override pnl.OnPaintBackground(...) {
}

}

Nope. You have to create your own subclass of Panel which overrides
OnPaintBackground, and then instantiate that instead.
 
Hi Peder,

As far as I know this is not possible. You will need to inherit the panel.

public class someClass
{
MyPanel pnl;

public someClass()
{
pnl = new MyPanel();
}
}

public class MyPanel : Panel
{
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
}
}
 
Ok, thanks yall!

I wanted to avoid inheriting the class itself, but it looks like I have
no other choice then...

- P -
 
Peder Y said:
I wanted to avoid inheriting the class itself, but it looks like I have
no other choice then...

Note that you /still/ can use your wrapper class if you're going for
encapsulation:

public class SomeClass
{
MyInheritedPanel panel;

public someClass() {
panel = new MyInheritedPanel();
}
}
 
Back
Top