PDA

View Full Version : Default/Optional Parameters...



Xorcist
05-03-2003, 03:05 AM
You can do this in Delphi right? Anyone want to gimme a quick lesson as to how.

cairnswm
05-03-2003, 04:39 AM
The way to create a procedure with default values is as follows:

Procedure Name(Var1 : Type1; Var2 : Type2 = Value2);

This procedure can be called in either of the following ways

Name(MyValue1);
or
Name(MyValue1,MyValue2);

The last parameters are the ones that can have default values you obviously cannot do the following:

Procedure Name(Var1 : Type1 = Value1; Var2 : Type2);


It is also possible to create the same procedure with different parameter lists (Overloaded procedures).

Here is a set of examples:
Function FullName(First : String; Second : String = 'Unknown') : String;
Begin
If Second = 'Unknown' then
Result := First
Else
Result := First+' '+Second;
End;

Function MakeName(FirstName : String) : String; Overload;
Begin
Result := FirstName;
End;
Function MakeName(FirstName, Surname : String) : String; Overload;
Begin
Result := FirstName+' '+Surname;
End;
Function MakeName(ID : Integer) : String; Overload;
Begin
Result := IntToStr(ID);;
End;



And to use them:

ShowMessage(FullName('William'));
ShowMessage(FullName('William','Cairns'));
ShowMessage(MakeName('William'));
ShowMessage(MakeName('William','Cairns'));
ShowMessage(MakeName(1234567890));

(Tested in Delphi 5)

TheLion
05-03-2003, 09:31 AM
I asked this question once on the Delphi e-list and everyone told me creating optional parameters like for example the WriteLn procedure is impossible and WriteLn can do it because it's handled differently by the compiler.

There was one bloke that told me a little trick to do it though and that was to use the override command and that's what I have been doing ever since! :)

Here is the example I got :

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure Say(N:Integer;S:string);overload;virtual;
procedure
Say(N:Integer;S1,S2:string);reintroduce;overload;
procedure
Say(N:Integer;S1,S2,S3:string);reintroduce;overloa d;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin

//I hope this is your problem
Say(1,'. Hello');
Say(2,'. Hello',' Armand');
Say(3,'. Hello',' Armand',' Postma!');

end;

procedure TForm1.Say(N:Integer;S:string);
begin
ShowMessage(inttostr(N)+S);
end;

procedure TForm1.Say(N:Integer;S1,S2:String);
var
S:string;
begin
S:=S1+S2;
ShowMessage(inttostr(N)+S);
end;

procedure TForm1.Say(N:Integer;S1,S2,S3:String);
var
S:string;
begin
S:=S1+S2+S3;
ShowMessage(inttostr(N)+S);
end;


end.

Okay it's not 100% a solution, more like a workaround, BUT sometimes workarounds are the only solutions, another trick you can use when it handles about the same types, is to use an array of string (or any other type) and pass the parameters between [xxx, xxx] :)

Alimonster
05-03-2003, 10:02 AM
You want to use overloaded procedures or default params, TheLion - much simpler a lot of the time, and probably more efficient. Default parameters and overloaded procs/funcs are available in >= Delphi 4 (which shouldn't be a problem, really).

Kim Friis
05-03-2003, 01:48 PM
With the first surgestion, you can also make several default params, but you have to have them as the last params.