Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

overloaded constructor on form 1

Status
Not open for further replies.

jovicat

Programmer
Sep 23, 2003
12
0
0
GR
I am using Delphi6. On one form wich I have to use it from
different places and with different parameters, I overload
the create method with some additional parameters.
e.g. 'constructor Create(aOwner:TComponent; byWho:TForma; ReadOnly:Boolean; DokBr, WHouse:String);overload;'.
and the implementation is :
{'constructor Create(aOwner:TComponent; byWho:TForma; ReadOnly:Boolean; DokBr, WHouse:String);
begin
inherited Create(aOwner);
fKoja:=byWho;
fReadOnly:=ReadOnly;
fDokBr:=DokBr;
fSklad:=WHouse;
tDokBr:=DokBr;
end;
}
In FormCreate, depending on parameters, when I call standard TForm.Create(Form) it's OK, but, when
I call overloaded create, I have access violation.
Error occurs when I try to access some TEdit object which is on the form from design time.
When I move part of code from FormCreate into OnShow event, I don't have error code. It seems that in FormCreate wich is inherited from my constructor, all objects are not created.
Does anyone have some clue about this problem.
 
First up, don't call the inherited Create for the Form - you want to call the other overloaded (original) Create constructor. It will in turn call the inherited Create.

See how that goes. I've always encountered little problems when I try to muck around with objects created in the design-time view.
 
I think Griffyn is right about calling the Create constructor instead of the inherited one. Also your problem may also be caused if you have overloaded TForm.Create(aOwner:TComponent)?
Code:
  //This is NOT correct
  constructor Create(aOwner:TComponent); overload; 
[\code]

I have had similar problems in the past. It is important to remember that the TComponent.Create(aOwner:TComponent) constructor is defined as virtual (for the right reasons). Therefore if you do decide to have your own Create(aOwner:TComponent) on a form you must mark it as override. 
[code]
  //This is correct
  constructor Create(aOwner:TComponent);override; 
[\code]

I am not sure if this will help, but hope it does. 


"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Hi bledazemi,

We probably won't ever know whether jovicat has solved the issue, but I wanted to clarify for you how override and overload works. The examples below show correct syntax.

This one is what usually happens when overriding a virtual method:
Code:
[b]type[/b]
  TForm1 = [b]class[/b](TForm)
  [b]public[/b]
    [b]constructor[/b] Create(AOwner: TComponent); [b]override[/b];
  [b]end[/b];

This one is if you override a virtual method with one using different parameters:
Code:
[b]type[/b]
  TForm1 = [b]class[/b](TForm)
  [b]public[/b]
    [b]constructor[/b] Create(New: String); [b]reintroduce[/b]; [b]virtual[/b];
  [b]end[/b];

This one is if you are overriding a virtual method, and introducing an overloaded one:
Code:
[b]type[/b]
  TForm1 = [b]class[/b](TForm)
  [b]public[/b]
    [b]constructor[/b] Create(AOwner: TComponent); [b]overload[/b]; [b]override[/b];
    [b]constructor[/b] Create(New: String); [b]reintroduce[/b]; [b]overload[/b]; [b]virtual[/b];
  [b]end[/b];

The virtual directive is not required on the overloaded method if you don't wish it to be virtual, regardless whether the original method was virtual or not.

The reintroduce directive simply suppresses the compiler warning that you are hiding a virtual method by declaring one with new parameters.
 
Great info. That warning "you are hiding a virtual method" has always bothered me. *4U

Roo
Delphi Rules!
 
Hi, Griffyn.
In Your first post, 'you want to call the other overloaded (original) Create constructor. It will in turn call the inherited Create', the problem is when I call the new create, e.g. Create(aOwner:TComponent; byWho:TForma; ReadOnly:Boolean; DokBr, WHouse:String), and like I noted before, J have error when I try to access one of the TEdit objects on the form.
On the form there are many objects and in the createform I give then init values depending on parameters. Strange thing is that some objects (TMonthCalendar, TButton) are accessed with no problems, but when I try to assign some value to the one of TEdit's, I have access viloation.

Constructor is same as above, only the line 'inherited Create(aOwner)' is the last one, because I need the parameters in 'create';

The onCreate code is something like :
procedure TFrmInt.FormCreate(Sender: TObject);
begin

...

if freadonly //parameter from constructor
..code ...
else
..other code ..

..
end;
 
I'm not understanding. Can you post some actual code?

If you're calling the form's own constructor within it's OnCreate event, you'll have problems because the form is already created.

There are sure to be many ways to do what you're trying to do. Post some code and we'll help you out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top