Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

optional parameters

dve83dve83 Posts: 30Member
is it possible to declare a function or procedure with optional parameters in delhi? or do i need to overload the method with two different sets of parameters?

thx in advance
d

Comments

  • zibadianzibadian Posts: 6,349Member
    : is it possible to declare a function or procedure with optional parameters in delhi? or do i need to overload the method with two different sets of parameters?
    :
    : thx in advance
    : d
    :
    It is possible to give parameters default values. This makes them optional in the call. Example: using a form with 1 label and 2 buttons
    [code]
    procedure MyShowMessage(Msg: string = 'Nothing to report');
    begin
    Form1.Label1.Caption := Msg;
    end;

    procedure TForm1.Button1Click(Sender : TObject)
    begin
    MyShowMessage; // no parameter
    end;

    procedure TForm1.Button2Click(Sender : TObject)
    begin
    MyShowMessage('Hello world'); // 1 parameter
    end;
    [/code]

    Using this method has its drawbacks however. If you have 2 or more parameters with default values each must be at the end of the procedure declaration:
    [code]
    procedure Foo(Index: integer = -1; Value: string = '');
    procedure Bar(Index: integer; Value: string = '');
    procedure FooBar(Index: integer = -1; Value: string); // <= Error
    [/code]
    Foo() and Bar() compile normally, while FooBar() doesn't compile. This also applies to calling the procedure:
    [code]
    Foo();
    Foo(10);
    Foo(10, 'Something');
    Foo('Something'); // <= Error
    [/code]

    One of the other alternatives is to use variant open array parameters:
    [code]
    // Declaration
    procedure Foo(const Args: array of const);

    // Call without parameters
    Foo([]);

    // Call with parameters
    Foo([10, @MyObject, 'Some string']);
    [/code]
    This however makes coding the procedure somewhat challenging, because it can have any number of parameters of any type. Variant open array parameters are detailed in the help files, and include an example of how to implement such a procedure.
  • dve83dve83 Posts: 30Member
    : : is it possible to declare a function or procedure with optional parameters in delhi? or do i need to overload the method with two different sets of parameters?
    : :
    : : thx in advance
    : : d
    : :
    : It is possible to give parameters default values. This makes them optional in the call. Example: using a form with 1 label and 2 buttons
    : [code]
    : procedure MyShowMessage(Msg: string = 'Nothing to report');
    : begin
    : Form1.Label1.Caption := Msg;
    : end;
    :
    : procedure TForm1.Button1Click(Sender : TObject)
    : begin
    : MyShowMessage; // no parameter
    : end;
    :
    : procedure TForm1.Button2Click(Sender : TObject)
    : begin
    : MyShowMessage('Hello world'); // 1 parameter
    : end;
    : [/code]
    :
    : Using this method has its drawbacks however. If you have 2 or more parameters with default values each must be at the end of the procedure declaration:
    : [code]
    : procedure Foo(Index: integer = -1; Value: string = '');
    : procedure Bar(Index: integer; Value: string = '');
    : procedure FooBar(Index: integer = -1; Value: string); // <= Error
    : [/code]
    : Foo() and Bar() compile normally, while FooBar() doesn't compile. This also applies to calling the procedure:
    : [code]
    : Foo();
    : Foo(10);
    : Foo(10, 'Something');
    : Foo('Something'); // <= Error
    : [/code]
    :
    : One of the other alternatives is to use variant open array parameters:
    : [code]
    : // Declaration
    : procedure Foo(const Args: array of const);
    :
    : // Call without parameters
    : Foo([]);
    :
    : // Call with parameters
    : Foo([10, @MyObject, 'Some string']);
    : [/code]
    : This however makes coding the procedure somewhat challenging, because it can have any number of parameters of any type. Variant open array parameters are detailed in the help files, and include an example of how to implement such a procedure.
    :


    thankyou. didn't think of default values. now I just got to way up default values vs overloading.

    thanks again
Sign In or Register to comment.