Board index » delphi » TRadioButton --> TButtonControl Type Casting(to Parent Class)

TRadioButton --> TButtonControl Type Casting(to Parent Class)

TButtonControl
          +
          +--------- TCustomCheckBox
          +                                  +
          +                                  +-------------TCheckBox
          +--------- TRadioButton

I Concerns about Class hierarchy like above.
I am curious of type casting of TRadioButton(or TCheckBox) to TButtonControl
TRadioButton(or TCheckBox) is creating dynamically on ScrollBox component
Some Source concerned is following

-------*< Source Begin >*----------
type
//Because TButtonControl is not dynamically instanced, first to be
descendant
   TBtnCtrl = class(TButtonControl);
    .......
var
    MyControl : TBtnCtrl; //Instance

procedure AAA();
begin
   ....
    //ScrollBox1.Controls[i] is dynamic control
    MyControl := (ScrollBox1.Controls[i] as TBtnCtrl);
    ....
end;
-------*< Source End >*----------

 

Re:TRadioButton --> TButtonControl Type Casting(to Parent Class)


Quote
"trytop" <a...@a.com> wrote:
>TButtonControl
>          +
>          +--------- TCustomCheckBox
>          +                                  +
>          +                                  +-------------TCheckBox
>          +--------- TRadioButton

>I Concerns about Class hierarchy like above.
>I am curious of type casting of TRadioButton(or TCheckBox) to TButtonControl
>TRadioButton(or TCheckBox) is creating dynamically on ScrollBox component

You can typecast any descendant to the type of an ancestor. So if you
have MyCheckBox of type TCheckBox, and MyRadioButton of type
TRadioButton, it is perfectly ok to reference:

  TButtonControl(MyCheckBox) or TButtonControl(MyRadioButton)

But there is no point in doing so. All the methods and properties
descend automatically.

If you are passed a control and you are not sure whether it is a
TCheckBox or a TRadioButton, then you can type cast it to a
TButtonControl, but you will lose access to the properties and methods
introduced by the descendants.

Good luck.

Kurt

Other Threads