Board index » delphi » Question - inheritance and type matching

Question - inheritance and type matching

Ok, here's a weird one -

Setup - I decided to start playing with MDI windows.  Ok, cool, open the
gallery and there's an application template already.  Pulled it out, added a
memo box to the MDIChild window, and got ready to get running....lets see, I
guess text alignment is a good place to start (ie. left/right/center).  I made
a procedure so the memo box can align it's self.

Now the snag - How do I activate the procedure?  I can't believe I'm the first
person to find this, but here's the problem...

1)  the main window is defined as
         TMainForm = class(TForm)
                property ActiveMDIChild : TForm;

2)  The MDI child is defined as
        TMDIChild = class(TForm)
                procedure SetLeftJust;
                procedure SetRightJust;
                procedure SetCenterJust;

3)  ActiveMDIChild refers to a variable of type TFORM, but the child windows
are of type TMDICHILD.  Therefore, even though I can use ActiveMDIChild.Close
(or any other standard TForm method), I cannot use the TMDIChild methods.  

The closest thing I've thought of was to override every method of TForm that
accesses the MDIChild properties, but that's just too unreasonable.  Surely
there must be a better way......right?

Brian

 

Re:Question - inheritance and type matching


Quote
brian.wil...@mhs.unc.edu (Brian Flynn) wrote:
>Ok, here's a weird one -
>Setup - I decided to start playing with MDI windows.  Ok, cool, open the
>gallery and there's an application template already.  Pulled it out, added a
>memo box to the MDIChild window, and got ready to get running....lets see, I
>guess text alignment is a good place to start (ie. left/right/center).  I made
>a procedure so the memo box can align it's self.
>Now the snag - How do I activate the procedure?  I can't believe I'm the first
>person to find this, but here's the problem...
>1)  the main window is defined as
>     TMainForm = class(TForm)
>            property ActiveMDIChild : TForm;
>2)  The MDI child is defined as
>    TMDIChild = class(TForm)
>            procedure SetLeftJust;
>            procedure SetRightJust;
>            procedure SetCenterJust;
>3)  ActiveMDIChild refers to a variable of type TFORM, but the child windows
>are of type TMDICHILD.  Therefore, even though I can use ActiveMDIChild.Close
>(or any other standard TForm method), I cannot use the TMDIChild methods.  
>The closest thing I've thought of was to override every method of TForm that
>accesses the MDIChild properties, but that's just too unreasonable.  Surely
>there must be a better way......right?
>Brian

The ActiveMDIChild property is declared as type TForm because Delphi
(Object Pascal) is a strongly typed language.  Therefore, you can only
access properties of type TForm with this property WITHOUT
typecasting.  What I suggest is trying the following:

If ActiveMDIChild is TMDIChild Then
  TMDIChild(ActiveMDIChild).SetLeftJust;

The "IF" statement determines if the data type of the ActiveMDIChild
is the form you're looking for (in this case, a TMDIChild).  If it is,
we will then typecast it and call the appropriate procedure.
Do not eliminate the IF statement.  If you incorrectly typecast an
object, you can GPF.

Mike Sherrane
sherr...@bluemtn.com                blue mountain software

Our motto: "Work hard,have fun,make money."
(Delphi,Progress,C++, & Rad.  Atlanta, Georgia. 770- 875-8324)

sherr...@bluemtn.com                blue mountain software

Our motto: "Work hard,have fun,make money."
(Delphi,Progress,C++, & Rad.  Atlanta, Georgia. 770- 875-8324)

Re:Question - inheritance and type matching


Quote
> Setup - I decided to start playing with MDI windows.  Ok, cool, open
> the gallery and there's an application template already.

[SNIP]
  Pulled it

Quote
> 3)  ActiveMDIChild refers to a variable of type TFORM, but the child
> windows are of type TMDICHILD.  Therefore, even though I can use
> ActiveMDIChild.Close (or any other standard TForm method), I cannot use
> the TMDIChild methods.  

You will need to typecast ActiveMDIChild. Basically, you are telling the
compiler, to belive that ActiveMDIChild will contain a MDI child at run
time.

  (ActiveMDIChild as TMDIChild).SetLeftJust;

should work, or

  TMDIChild(ActiveMDIChild).SetLeftJust;

Note the first example is safer, it checks the type of ActiveMDIChild,
and will generate an exception if it isn't of the right type.  

----------------------------------------
Nick Moon  ncm...@cix.compulink.co.uk
----------------------------------------

Other Threads