"Tabulator" wrote ...
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