Results 1 to 5 of 5

Thread: Default/Optional Parameters...

  1. #1

    Default/Optional Parameters...

    You can do this in Delphi right? Anyone want to gimme a quick lesson as to how.
    My DGDev forum pascal syntax highlight settings:
    <br />[background=#FFFFFF][comment=#8080FF][normal=#000080]
    <br />[number=#C00000][reserved=#000000][string=#00C000]

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Default/Optional Parameters...

    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)
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Default/Optional Parameters...

    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 :

    [pascal]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,S2tring);
    var
    S:string;
    begin
    S:=S1+S2;
    ShowMessage(inttostr(N)+S);
    end;

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


    end.[/pascal]

    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]
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  4. #4

    Default/Optional Parameters...

    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).
    "All paid jobs absorb and degrade the mind."
    <br />-- Aristotle

  5. #5

    Default/Optional Parameters...

    With the first surgestion, you can also make several default params, but you have to have them as the last params.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •