IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismLanguage Det...   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Prism Language Details:
Delphi Prism Overloading (implicit)
 
Posted 16 years ago on 2/13/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

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.

More Info

Definition:  Overloading

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1212A1
Enter key:
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

 KB Article #101903 Counter
19011
Since 2/13/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2025 Prestwood IT Solutions.   [Security & Privacy]