TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Overload and Default Parameters in Action
 
   
 Posted: 14 August 2000
 
   
 
 Applies to: Delphi 4.0 and later
 
       
   

Introduction

As developers, we're often told to write our code so it can be easily reused for later projects. In practice, though, we often find reasons for not using using code written for an earlier application. We have many excuses, but the end result is the same. We often end up rewriting code designed to solve the same problem.

Starting with Delphi 4, however, we have fewer "real" reasons for doing so. Inprise added two extensions to Object Pascal that make it much easier to create flexible interfaces:

  1. The overload directive lets you implement multiple interfaces for a given routine (method, procedure, or function.)

  2. Parameters can now have default values, effectively making it possible for you to ignore one or more parameters when calling your library routines.

While Delphi's Help files do a good job of describing these new features, the examples aren't entirely practical. Worse, they can be confusing if you're not familiar with these concepts from other languages or if you're new to programming in general.

The following code fragment both of these concepts in action and also provides a convenience for those with experience in ObjectPAL, the programming language for Paradox for Windows. It shows how to implement a version of msgInfo(), a shortcut for the messageBox() API function which creates a short message dialog containing a text string and an OK button.

unit MsgBox32;

interface

uses
   Forms, SysUtils, Windows;

const
   MSGOPTDEF = MB_APPLMODAL;
   MSGOPTINF = MSGOPTDEF + MB_OK          + MB_ICONINFORMATION; // msgInfo options

   MSGCAPINF = 'Information';

function mbWrapper( strCaption, strContent : String;
                    intOptions : Integer ) : Integer;

function msgInfo( strCaption, strContent : String;
                   intOptions : Integer = MSGOPTINF ) : Integer; overload;
function msgInfo( strContent : String;
                   intOptions : Integer = MSGOPTINF ) : Integer; overload;

implementation

function mbWrapper( strCaption, strContent : String;
                    intOptions : Integer ) : Integer;
{ -------------------------------------------------------------------------
  This function is a wrapper for the MessageBox function; it handles the
  data conversion details, calls the function, and then returns the user's
  response to the calling process.
  ------------------------------------------------------------------------- }
var
   pcContent,          { Content of the dialog box, e.g. the text message }
   pcCaption : pChar;  { Caption of the dialog box, e.g. the title }
   strEdited : string; { Edited version of content }
   strLastCh : string; { last character of content }
begin

   if copy( strContent, length( strContent ) - 1, 2 ) = '..' then
      strEdited := copy( strContent, 1, length( strContent ) - 1 )
   else
      strEdited := strContent;

   strLastCh := copy( strEdited, length( strEdited ), 1 );
   if ( strLastCh <> '.' ) AND ( strLastCh <> '?' ) AND
      ( strLastCh <> '!' ) then
      strEdited := strEdited + '.';

   pcContent := pChar( strEdited );
   pcCaption := pChar( strCaption );

   result := Application.messageBox( pcContent, pcCaption, intOptions );

end; { function }


function MsgInfo( strCaption, strContent : String;
                  intOptions : Integer = MSGOPTINF  ) : Integer;
{ -------------------------------------------------------------------------
  This function provides a general function for constructing and displaying
  informational messages.
  ------------------------------------------------------------------------- }
begin

   If strCaption = '' then
      result := MBWrapper( MSGCAPINF, strContent, intOptions )
   else
      result := MBWrapper( strCaption, strContent, intOptions );

end;

function MsgInfo( strContent : String;
                  intOptions : Integer = MSGOPTINF  ) : Integer;
begin
   result := msgInfo( '', strContent, intOptions );
end;

end.

Download Full Unit

When reviewing the unit, keep the following points in mind:

  • The full unit contains eight sets of shortcut dialogs are provided, each designed for a different situation.

  • Each dialog, except sny(), supports up to three parameters: strCaption (the title), strContent (the text in the dialog) and intOptions (the flags passed to the messageBox() API function.

  • You can call each function with a single string parameter: strContent. When this happens, the unit assumes an appropriate title for the final dialog. This is accomplished with the overload directive.

  • The intOptions parameter is optional. If not specified, the code assumes reasonable (in our opinion) flags. This is done with the default directive.

  • For convenience, the unit added punctuation to the end of strContent, if none is provided.

  • International users can replace the constants with text appropriate for their locales.

  • Unfortunately, the overload directive cannot be used to change the result type of a functions; otherwise, the prompts would return logical values.

  • Notice that only one function does the real work. This reduces the amount of long term maintainance required to modify the behavior of the entire unit. For example, should Microsoft choose to add new functionality to messageBox, it would be trivial to adapt this unit (and all uses of it) to incoporate those changes.

  • The following code sample contains a fragment of the full unit. Download the sample for the complete unit, including comments.

Conclusion

While perhaps trivial, this code demonstrates how you can leverage the overload and default directives to create more flexible library routines. in doing so, you create code that's easier to maintain and reuse. In turn, this reduces the amount of time and money needed to develop applications quickly. Perhaps it's not so trivial after all. ;-)

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]