Title: Overloading Delphi's ShowMessage to accept Integer, Boolean, Float, Currency
Delphi's ShowMessage procedure, defined in the dialogs.pas unit, displays a message in a dialog box, waits for the user to click an OK button.
I'm using the ShowMessage procedure frequently while developing applications as a "debugging" tool to display values of various variables, properties, function results.
A downside of the ShowMessage is that it only accepts a string parameter - where variables tend to be integers, bools, floats, etc. :)
Everytime I want to display an integer value using ShowMessage I first need to convert the integer value to a string (using IntToStr), so that the call to the ShowMessage for an integer value looks like: ShowMessage(IntToStr(2008)), for example.
A smarter, more flexible ShowMessage
To speed up my coding I've added a few overloaded ShowMessage procedures, put them all in a unit called "common.pas" - I'm sure you also have dozens of units you use in your every project where you have stored your custom created "helper" types, functions and procedures.
Here's the common.pas (only ShowMessage implementations):
unit common;
interface
uses dialogs, sysutils;
procedure ShowMessage(const value : string) ; overload;
procedure ShowMessage(const value : integer) ; overload;
procedure ShowMessage(const value : extended) ; overload;
procedure ShowMessage(const value : boolean) ; overload;
implementation
//displays a string in a dialog box
procedure ShowMessage(const value : string) ;
begin
Dialogs.ShowMessage(value) ;
end;
//displays an integer in a dialog box
procedure ShowMessage(const value : integer) ;
begin
ShowMessage(IntToStr(value)) ;
end;
//displays a float in a dialog box
procedure ShowMessage(const value : extended) ;
begin
ShowMessage(FloatToStr(value)) ;
end;
//displays a boolean in a dialog box
procedure ShowMessage(const value : boolean) ;
begin
ShowMessage(BoolToStr(value, true)) ;
end;
end.
Note that this unit lists "dialogs" in the interface uses clause - and that the version that accepts the string actually calls the "original" ShowMessage from the (standard) dialogs.pas unit.
Other overloads (integer, boolean and extended) use RTL functions to convert a value to string (like FloatToStr and BoolToStr).
How to use ShowMessage overloads
If you want to use this unit from any other unit, you of course have to include it in the uses clause, plus:
Make sure "common" is listed AFTER dialogs (if you already have dialogs unit listed) in the uses section of the calling unit.
Here's an example:
uses dialogs, common;
begin
ShowMessage(2008) ;
ShowMessage(true) ;
end;