1. Home
  2. Computing & Technology
  3. Delphi Programming

Overloading Delphi's ShowMessage to accept Integer, Boolean, Float, ...
Create overloaded ShowMessage procedures to accept non-string parameter

By Zarko Gajic, About.com

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 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;

Delphi tips navigator:
» Class Helper for Delphi's TStrings: Implemented Add(Variant)
« Create a Custom Message Dialog with Standard Windows Icons using Delphi

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. Overloading Delphi's ShowMessage to accept Integer, Boolean, Float, Currency ...

©2009 About.com, a part of The New York Times Company.

All rights reserved.