Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-04

OOP basics

14 messages · 11 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kjow

Hi all,

I need to pass a my custom TMainForm to a function on other unit, e.g.

main unit:

...
uses

  Classes, SysUtils, FileUtil, LResources, Forms, Controls, test;
type

TMainForm

  TMainForm = class(TForm)
    ...
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
    x:integer;
    ...
  public
    { public declarations }
    y: integer;
    ...
  end;

_______

Other unit "test.pas":

unit test;
{$mode objfpc}{$H+}
interface
uses
  third;
procedure init(...; ...; var Form: TMainForm);
implementation
procedure init(...; ...; var Form: TMainForm);
begin
...
Form.y:=0;
end;

_______

Is it possible?

PS I'm sorry for this example, maybe it is not perfect, I wrote it on the fly.

Thanks,
Kjow

Reply by email

Paul Ishenin
to Kjow
Kjow wrote
procedure init(...; ...; var Form: TMainForm);

You don't need to pass Form by reference (using 'var') since class instance is pointer. This is only required if you want to replaced passed Form argument by another class instance.

Best regards,
Paul Ishenin.

Reply by email

Graeme Geldenhuys
to Kjow: I need to pass a my custom TMainForm to a function on other unit, e.g. main unit: ... uses
Kjow wrote
Is it possible?

Yes, but there is an alternative too. Very non-OOP, but Lazarus and Delphi enforce it for form creation (and requirement by form designer). In your unit with the TMainForm, there should be a lovely global instance variable called MainForm that you can access from everywhere. Also not very OOP like, all controls on the TMainForm (and any other form created with Delphi and Lazarus) is published, so they are globally accessible too.

All this is not what I call good OOP design, but hey, it's RAD convenience. :-/

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Unknown author
to Graeme Geldenhuys
Graeme Geldenhuys wrote 9 lines
On 14 April 2010 13:09, Kjow wrote: > > Is it possible? Yes, but there is an alternative too. Very non-OOP, but Lazarus and Delphi enforce it for form creation (and requirement by form designer). In your unit with the TMainForm, there should be a lovely global instance variable called MainForm that you can access from everywhere.

Little bit OT:

Is this global var really neccessary? Normally I delete all those variables in my Delphi projects and create my forms on the fly and everything works fine.

Is this really a requirement for the lazarus form designer?

regards,

Stefan

---------------------------------------------------------------------------- Absender: Kommunikation : iret GmbH web : www.iret.de Stefan Hille email: Paul-Engelhard-Weg 50a voice: +49 251 615138 D-48167 Muenster fax: +49 251 617631 Vollstaendige Firmenbezeichnung iret Gesellschaft zur Entwicklung elektronischer Steuerungen mbH H?ltenweg 103 48155 M?nster Amtsgericht M?nster, HRB 3724 Gesch?ftsf?hrer: Renate Rittmeyer-M?ller Stefan Hille

Reply by email

Alexander Klenin
to Unknown author
Show quoted text 18 lines
On Wed, Apr 14, 2010 at 01:53:55PM +0200, Graeme Geldenhuys wrote: > On 14 April 2010 13:09, Kjow ?wrote: > > > > Is it possible? > > Yes, but there is an alternative too. Very non-OOP, but Lazarus and > Delphi enforce it for form creation (and requirement by form > designer). In your unit with the TMainForm, there should be a lovely > global instance variable called MainForm that you can access from > everywhere. Little bit OT: Is this global var really neccessary? Normally I delete all those variables in my Delphi projects and create my forms on the fly and everything works fine. Is this really a requirement for the lazarus form designer?

See "When createing new forms, add them to auto-created forms" checkbox in Project Options.

Alexander S. Klenin

Reply by email

Graeme Geldenhuys
to Unknown author: Little bit OT: Is this global var really neccessary? Normally I delete all those…
Show quoted text 6 lines
Is this global var really neccessary? Normally I delete all those variables in my Delphi projects and create my forms on the fly and everything works fine. Is this really a requirement for the lazarus form designer?

The global variable is not a requirement and can be disabled by removing the "Auto create forms" in the Project Options dialog.

Published components and event handlers however are a "lazarus/delphi forms designer" requirement - which means everything you drop on a form is globally visible. I hate that design.

fpGUI's Form Designer on the other hand does not force such a design. By default all my components and event handlers for a form are private. If I want to make some element of the data stored in a component visible, I create a public property declaration for the specific data - never the whole component.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

José Mejuto
to Unknown author: Little bit OT: Is this global var really neccessary? Normally I delete all those…

Hello Lazarus-List,

Thursday, April 15, 2010, 9:25:06 AM, you wrote:

smd> Little bit OT:
smd> Is this global var really neccessary? Normally I delete all those smd> variables in my Delphi projects and create my forms on the fly and smd> everything works fine.
smd> Is this really a requirement for the lazarus form designer?

AFAIK only for autocreated forms, in my case usually only the main form, I do not like the autocreated ;)

Best regards, Jos?

Reply by email

Marc Weustink
to Graeme Geldenhuys: The global variable is not a requirement and can be disabled by removing the "Auto create…
Graeme Geldenhuys wrote 14 lines
On 15 April 2010 09:25, wrote: > > Is this global var really neccessary? Normally I delete all those > variables in my Delphi projects and create my forms on the fly and > everything works fine. > > Is this really a requirement for the lazarus form designer? The global variable is not a requirement and can be disabled by removing the "Auto create forms" in the Project Options dialog. Published components and event handlers however are a "lazarus/delphi forms designer" requirement - which means everything you drop on a form is globally visible. I hate that design.

Yes and no
The lazarus/delphi forms designer uses RTTI for created components. RTTI is only created for published members of a class so for that they need to be published.

from a RTTI pov this seems logical since with RTTI you already have all info available. But from a class design pov it would have been nice if there was a 2nd way to tell the compiler to generate RTTI for some non-published members

Marc

Reply by email

Graeme Geldenhuys
to Marc Weustink: Yes and no The lazarus/delphi forms designer uses RTTI for created components. RTTI is…
Marc Weustink wrote
The lazarus/delphi forms designer uses RTTI for created components. RTTI is only created for published members of a class so for that they need to be published.

That's exactly why fpGUI UI Designer doesn't use RTTI. Down side is you need to tell the UI Designer what should be available in the Object Inspector. Up side is that the controls on the forms or properties of a component can have any visibility, and the UI Designer will still work. And as a "catch all" where you need to set a property, but it isn't in the Object Inspector, there is a 'Unknown Lines' memo in the Object Inspector that will store whatever you typed in there exactly as is, in the generated code. And it also does the reverse, when loading a form or component properties it doesn't know about, it adds those to the Unknown Lines memo.

available. But from a class design pov it would have been nice if there was a 2nd way to tell the compiler to generate RTTI for some non-published members

I guess that is where Java and .NET's Reflection is better than FPC's RTTI. With Reflection you can accesss anything information about a class (well so I hear - I don't use Java or .NET).

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email

Marco van de Voort
to Graeme Geldenhuys
Graeme Geldenhuys wrote
I guess that is where Java and .NET's Reflection is better than FPC's RTTI. With Reflection you can accesss anything information about a class (well so I hear - I don't use Java or .NET).

Now you played that card, you are never allowed to complain about either binary sizes, or memory use again :-)

Reply by email

Flávio Etrusco
to Graeme Geldenhuys: That's exactly why fpGUI UI Designer doesn't use RTTI. Down side is you need to tell the…
Graeme Geldenhuys wrote 16 lines
On 15 April 2010 15:31, Marc Weustink wrote: > The lazarus/delphi forms designer uses RTTI for created components. RTTI is > only created for published members of a class so for that they need to be > published. That's exactly why fpGUI UI Designer doesn't use RTTI. Down side is you need to tell the UI Designer what should be available in the Object Inspector. Up side is that the controls on the forms or properties of a component can have any visibility, and the UI Designer will still work. And as a "catch all" where you need to set a property, but it isn't in the Object Inspector, there is a 'Unknown Lines' memo in the Object Inspector that will store whatever you typed in there exactly as is, in the generated code. And it also does the reverse, when loading a form or component properties it doesn't know about, it adds those to the Unknown Lines memo.

When visibility is really a problem, you can use TComponent.DefineProperties...

Best regards,
Fl?vio

Reply by email

Graeme Geldenhuys
to Flávio Etrusco

2010/4/15 Fl?vio Etrusco :

When visibility is really a problem, you can use TComponent.DefineProperties...

Or simply use a "property types" (aka ValueType) framework, that doesn't use RTTI at all, yet you can query all properties and even add new have used this in combination with tiOPF
before and it works well.

Regards, - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://opensoft.homeip.net/fpgui/

Reply by email