delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


Boolean to String ConversionFormat this article printer-friendly!Bookmark function is only available for registered users!
Convert your boolean values to the meaningful words
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
11/24/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet boolean string conversion
Times Scored:
12
Visits:
8844
Uploader: Dmitri Papichev
Company:
Reference: N/A
 
Question/Problem/Abstract:
How I convert boolean values to the words depending on the situation? For example, TRUE here means "Enabled", and FALSE there means "Failed"?
Answer:



Here is the code snippet to do the job.
If the second parameter is omitted, the function returns "TRUE" or "FALSE".
Modify the function declaration to change the default returning values.
Expand TBooleanWordType and BooleanWord definitions to include more specific values if needed.

interface
[...]
type
   TBooleanWordType =
      (bwTrue, bwYes, bwOn, bwEnabled, bwSuccessful, bwOK, bwOne);
[...]
function BoolToStr (AValue: boolean;
                    ABooleanWordType: TBooleanWordType = bwTrue): string;
[...]
{=====================================================}
implementation
[...]
const
   BooleanWord: array [boolean, TBooleanWordType] of string =
      (
       ('FALSE', 'No',  'Off', 'Disabled', 'Failed',     'Cancel', '0'),
       ('TRUE',  'Yes', 'On',  'Enabled',  'Successful', 'OK',     '1')
      );

[...]
{-----------------------------------------------------}
function BoolToStr (AValue: boolean;
                    ABooleanWordType: TBooleanWordType = bwTrue): string;
begin
   Result := BooleanWord [AValue, ABooleanWordType];
end; {--BoolToStr--}
[...]





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
a 2nd version
    Andreas Schmidt (Nov 26 2001 6:55PM)

interface

function BoolToStr(b:boolean; TrueValue:string='1'; FalseValue:string='0') : string;overload;

implementation

function BoolToStr(b:boolean; TrueValue:string='1'; FalseValue:string='0') : string;overload;
begin
     if b then Result:=TrueValue else Result:=FalseValue,
end;


// example for italian language
s := BoolToStr(CheckBox1.Checked, 'Si', 'No');

Add this overloaded Function (by Marian Aldenhoevel)
to the unit.

Respond

RE: a 2nd version
Dmitri Papichev (Jan 14 2002 10:35PM)

Thank you for the suggestion, Andreas (better late than never :-)
That could be very useful indeed for the rare descriptions, and complements the first function perfectly.
Respond

RE: RE: a 2nd version
Carlos Alberto Longen (Mar 5 2002 11:01PM)

you also can do the following:

const
  arrBooleanValues: array[Boolean] of ShortString = ('False', 'True');
var
  b: Boolean;
  s: String;
begin
  b := False;
  s := arrBooleanValues[b];  // 'False'
  b := True;
  s := arrBooleanValues[b];  // 'True'
end;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Hans Gulö
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)