|
Technical answers from the trenches |
|
Overload and Default Parameters in Action
| ||||
Posted: 14 August 2000 |
||||
  |
Applies to: Delphi 4.0 and later |
|||
IntroductionAs 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:
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.
When reviewing the unit, keep the following points in mind:
ConclusionWhile 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. ;-) |
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|