Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing Extra Params to Form Constructor

0 views
Skip to first unread message

Martin James

unread,
Apr 21, 2003, 11:01:20 AM4/21/03
to

Tommy <to...@here.now> wrote in message
news:eru7avoltv6hvia70...@4ax.com...
> How could I pass an extra paramater to a form's constructor?
>
> At the moment I'm doing something like this:
>
> procedure CreateNewForm;
> var
> SomeForm: tSomeForm;
> begin
> SomeForm := tSomeForm.Create(MainForm);
> SomeForm.SomeProperty := 5;
> SomeForm.Show;
> { etc... }
> end;
>
> ... but I'd like to say something like:
>
> SomeForm := tSomeForm.Create(5); // instead of SomeProperty := 5
> SomeForm.Show;
>
> ... just to make my code more tidy. What would be the best way to
> implement something like that?

You could just add a new constructor:

TmyForm=class(Tform)
protected
myParameter:integer;
public
constructor myCreate(anOwner:Tcomponent;aParameter:integer);
end;

or you could overload create:

TmyForm=class(Tform)
protected
myParameter:integer;
public
constructor Create(anOwner:Tcomponent;aParameter:integer);
reintroduce; overload;
end;

in either case, call the inherited create:

constructor myForm.create(anOwner:Tcomponent;aParameter:integer);
begin
inherited create(anOwner);
myParameter:=aParameter;
end;


Then you can instantiate myForms:

thisForm:TmyForm;

thisForm:=TmyForm.create(self,5);

Rgds,
Martin

Sven Pran

unread,
Apr 21, 2003, 12:43:21 PM4/21/03
to

"Tommy" <to...@here.now> wrote in message
news:1s68av4nb38dfrv6q...@4ax.com...

> In article <3ea4...@newsgroups.borland.com>, Martin James wrote:
>
> >TmyForm=class(Tform)
> >protected
> > myParameter:integer;
> > public
> > constructor Create(anOwner:Tcomponent;aParameter:integer);
> >reintroduce; overload;
> >end;
>
> Thanks Martin

I believe I saw something recently that this way of creating your instance
of TmyForm could (will?) fail if it is actually dropped on the mainform and
as such created automatically during mainform.create. I don't remember
the details, but there was something to the effect that your "private"
constructor is not called in such cases, only the "inherited" constructor
"create" is called.

Better investigate, I hope for your sake that I am wrong.

regards Sven


Martin James

unread,
Apr 21, 2003, 5:05:08 PM4/21/03
to

>
> I believe I saw something recently that this way of creating your
instance
> of TmyForm could (will?) fail if it is actually dropped on the
mainform and
> as such created automatically during mainform.create. I don't
remember
> the details, but there was something to the effect that your
"private"
> constructor is not called in such cases, only the "inherited"
constructor
> "create" is called.
>

Woudn't be suprised. I've only ever considered using overloaded form
constructors when dynamically creating the forms at runtime.

Rgds,
Martin


0 new messages