From the January 2016 Issue of Prestwood eMag
 
Prism Language Details:
Delphi Prism Overloading (implicit)
 
Posted 2/13/2009 on 2/13/2009
Prism Code Snippet:

KB101903

General Info: Overloading

Types of overloading include method overloading and operator overloading.

Method Overloading is where different functions with the same name are invoked based on the data types of the parameters passed or the number of parameters. Method overloading is a type of polymorphism and is also known as Parametric Polymorphism.

Operater Overloading allows an operator to behave differently based on the types of values used. For example, in some languages the + operator is used both to add numbers and to concatenate strings. Custom operator overloading is sometimes referred to as ad-hoc polymorphism.

Delphi Prism Overloading

Like Delphi, Prism supports overloading. However, Prism supports implicit overloading (no need for an overload keyword).

Syntax Example:
method MainForm.Add(a, b: integer): Integer;
begin
Result := a + b;
end;
  
method MainForm.Add(const msg: String; a, b: integer): String;
begin
Result := msg + (a + b);
end;

Delphi Prism Working Example

In the following demo, we will add the two overloaded methods above to a form class and use them in a button click event.

  1. Create an application with a single form and button.
  2. Alter the form class in the interface section as follows:
    MainForm = partial class(System.Windows.Forms.Form)
    private
      method button1_Click(sender: System.Object; e: System.EventArgs);
    protected
      method Dispose(disposing: Boolean); override;
    public
      constructor;
     
      method Add(a, b: integer): Integer;
      method Add(const msg: String; a, b: integer): String;

    end;

     
  3. Add the two methods to the implementation section:
    method MainForm.Add(a, b: integer): Integer;
    begin
      Result := a + b;
    end;
     
    method MainForm.Add(const msg: String; a, b: integer): String;
    begin
      Result := msg + (a + b);
    end;

     
  4. Now exercise our overloaded method. Alter the click event of our button as follows:
    method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
    begin
      MessageBox.Show("2 + 2 = " + Add(2,2));
      MessageBox.Show(Add("3+4=",3,4));

    end;

     
  5. Test-it. Run the application and click the button.
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile
www.prestwood.com For service: 916-726-5675
or support@prestwood.com
Copyright (C) Prestwood IT Solutions.
All Rights Reserved.
Printed 12/6/2021