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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing argument?

Status
Not open for further replies.

strucnjak79

Programmer
Oct 14, 2005
37
BA
Well I think you will find my question stupid, but I'm beginer, so plz help!

I need to make one form for several code forms... They all looks allmost the same, with code and name fields, and with grid to show existing data! For now it's ok! But I have city code, country code, street code and few more, so I dont want to make 10 forms, I just want to pass an argument to my form, so my code form will know which table in db to use!

This is how I create my form:

'if not assigned(Form1) then
frmSifarnik:=TfrmSifarnik.Create(Application);
frmSifarnik.Show;'

How to pass that data in new form?


Second, some of this tables has mote than two fields, maby two of them while others has only code and name fields... So can I use the same form for tables with 2 and tables with 3 field, and how? Can I hide or not create some fields if I dont need them?
 
I havnt tried this myself yet , but wont the 'Forms' component be ideal for this sort of thing.

Other than that you have to cast stuff to the created object.
e.g
with (frmSifarnik as TfrmSifarnik) do
begin
// reference the objects on the form
end



Steve: Delphi a feersum engin indeed.
 
You have a lot of options, but the two better ones are:

1) Define a public property in the form to set the parameter, creating an accessor method to do the work:

Code:
[b]Form definition[/b]
type
TMyForm = class(TForm);
private
  FParam : TMyParamType;
  procedure SetParam(P : TParamType); // Accessor method
public
  property MyParameter : TParamType read FParam write SetParam;
end;

[b]Form implementation[/b]
TMyForm.SetParam(P : TParamType);
begin
  FParam := P;
  Do_whatever_is_needed_to_personalize_the_form;
end;

[b]Using the form[/b]
MyForm := TMyForm.Create(Self); // or (Application) or (nil)
MyForm.MyParameter := ParameterValue; // Tell the form what it is
MyForm.Show.

2) Redefine the form constructor:

Code:
[b]Form definition[/b]
type
TMyForm = class(TForm);
public
  constructor Create(AOwner    : TComponent; 
                     Parameter : TParameterType); [COLOR=red]reintroduce[/color];
end;

[b]Form implementation[/b]
constuctor TMyForm.Create(AOwner : TComponent; Parameter : TParameterType);
begin
  inherited Create(AOwner);
  Do_whatever_is_needed_to_personalize_the_form;
end;

[b]Using the form[/b]
MyForm := TMyForm.Create(Self, ParameterValue);
MyForm.Show;

The constructor redefinition is the best method. If you have a dozen of parameters it is likely to forget to set one property in the first approach, but the compiler is working on your side in the second one.

Futhermore, in some cases you need to use several parameters at same time, like in:

if (Param1 = xx) and (Param 2 = yy) then...

In this scenario, passing the parameters one by one via properties is the wrong approach, as you need to define a "property loading sequence" (property XX needs to be last to property XX, or property ZZ need to be last of all) what is a very bad programmer practice.

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top