"overclassing"

  • Thread starter Thread starter Tabulator
  • Start date Start date
T

Tabulator

Hi

it's possible to "overclass" (at runtime) a base control with a new instance
of a derived class?

Thanks
 
Tabulator said:
it's possible to "overclass" (at runtime) a base control with a new instance
of a derived class?

What exactly do you mean? If you could give more explanation about what
you're trying to do, we might be able to help.
 
I'm sorry but my english isn't so good...

An example:
I have a Button on a form (class Button)
In the project I have a control derived from Buttom (DerivedButton)

I need to use at runtime the button like it is a DerivedButton.

(In win32 is a kind of subclassing...)
 
Tabulator said:
I'm sorry but my english isn't so good...

An example:
I have a Button on a form (class Button)
In the project I have a control derived from Buttom (DerivedButton)

I need to use at runtime the button like it is a DerivedButton.

(In win32 is a kind of subclassing...)

You certainly can't treat an instance of just the base class as if it
were an instance of the derived class, no.
 
...
I have a Button on a form (class Button)
In the project I have a control derived
from Buttom (DerivedButton)

I need to use at runtime the button like
it is a DerivedButton.

You can in most cases not cast *down* from a superclass to a subclass, but
there's at least two other approaches you can use to solve this:

1. You can *replace* an initial Button
with a DerivedButton in runtime.

As an example, if you have a Button in your form, such as:

private Button myButton = new Button();

You can anytime during execution exchange that for an instance of a subclass
to Button, e.g. DerivedButton:

myButton = new DerivedButton();

(a variable of a supertype can reference to an instance of any of its
subtypes)

Just remember to do the rest of possibly needed initiation for the new
button, such as Location, EventHandlers, etc.

However, the most obvious and easiest approach is:

2. Use the DerivedButton from start!
I can't see any reason to why not, as the
DerivedButton inherits all of the behaviour
from Button.

// Bjorn A
 
Back
Top