[Lazarus] OOP basics - 2

José Mejuto joshyfun at gmail.com
Wed Apr 14 19:08:44 CEST 2010


Hello Lazarus-List,

Wednesday, April 14, 2010, 6:13:22 PM, you wrote:

K> But, can I implement functions/methods of "designed form" on others units?
K> If I need to access to a component resource of the "design form", I
K> need to implement it on the unit that contain:
K> TForm1=class(TMyBaseForm);
K> right?
K> So, if I need to access to the properties of a Tbutton that is on the
K> "designed form", I can't... is it right? How to do?
K> The question is similar to the one I asked at 16.38.

Let me answer in a different way ;) If you need to access a futurable
component in the form, something is wrong in your design ;)

If you would need to access a control it must be designed in
TMyBaseForm, the problem is that I think it can not be visually
designed (I think this is called VFI aka Visual Form Inheritance) and
I think it is not supported or not well supported currently in
Lazarus.

If you know, in example, that you will need to access a TMemo, because
all forms have a TMemo with a log of what is happening then you can
create in the BaseForm a dummy TMemo reference to be used by the
functions and just in the descendent setup a reference in the
FormCreate event:

type
  TMyBaseForm=class(TForm)
  protected
    TMemoLog: TMemo;
    procedure AddToLog(text: String);
  end;
  .....
procedure TMyBaseForm.AddToLog(text: String);
begin
  if not Assigned(TMemoLog) then exit;
  TMemoLog.Lines.Add(text);
end;

...................................
type
  TForm1=class(TMyBaseForm)
  procedure FormCreate(Sender: TObject);
  .....
TForm1.procedure FormCreate(Sender: TObject);
begin
  TMemoLog:=Memo1;
end;
...................................

You should not implement a function/procedure using futurable present
objects, unless you like to play with castings and the nice AV when
casting a button to a memo because the names where exchanged :(

If you plan to use dynamic add/remove objects then the code would be a
bit more complex because the free of Memo1 and set to nil will not be
inherited by TMemoLog so you should proceed with care.

The advantage of this is that you keep a type control of everything
without nasty castings.

-- 
Best regards,
 José





More information about the Lazarus mailing list