Jerry Dodge
Jerry Dodge

Reputation: 27276

Is there an easier way to do boolean conversions?

I have used this scenario many times in nearly all my projects, when I'm doing some sort of data conversion, when it comes to booleans, I kinda get a little lost when it comes to making it simple. This statement below sticks out like a sore thumb all over my code:

if BoolVal then
  StrVal:= 'True'
else
  StrVal:= 'False';

I'm wondering if there's an easier way to perform this evaluation? Perhaps some use of the Case statement I don't know about? My actual implementation is more complex than just StrVal but it does consist of returning two different values depending on whether it's True or False. For example, here's some real code...

if fsBold in Can.Font.Style then
  ConvertTo(AddSomeOtherText + 'True')
else
  ConvertTo(AddSomeOtherText + 'False');

That's just to emphasize on how simple I'm hoping. I'm wondering if I can do something along the lines of this:

ConvertTo(AddSomeOtherText + BoolToStrCase((fsBold in Can.Font.Style), 'True', 'False'));

I'm sure that's not a real command, but I'm looking for that type of simplicity in one single line.

Upvotes: 21

Views: 37356

Answers (5)

Wes
Wes

Reputation: 420

If you're into obtuse code, here's a fun way to do it (especially of it's part of a larger Format statement), but be careful if you have more arguments following (or preceding), you will have to index the argument following the boolean explicitly (told you it was obtuse):

Format('The value of value is %*:s', [Integer(value)+1, 'False', 'True']);

Anyone caught using this in production code should be dealt with severely!

Upvotes: 3

Eric Santos
Eric Santos

Reputation: 131

Try either of these. Both are way faster than default versions.

type
 TBooleanWordType = (bwTrue, bwYes, bwOn, bwEnabled, bwSuccessful, bwOK, bwBinary);

 BooleanWord: array [Boolean, TBooleanWordType] of String =
  (
    ('False', 'No',  'Off', 'Disabled', 'Failed',     'Cancel', '0'),
    ('True',  'Yes', 'On',  'Enabled',  'Successful', 'Ok',     '1')
  );

function BoolToStr(Value: boolean; const BooleanWordType: TBooleanWordType = bwTrue): String; inline;
begin
   Result := BooleanWord[Value, BooleanWordType];
end;

function StrToBool(const S: String): Boolean; inline;
begin
  Result := False;
  case Length(S) of
    4: Result := (LowerCase(S) = 'true');
    5: Result := not (LowerCase(S) = 'false');
  end;
end;

Upvotes: 3

Marjan Venema
Marjan Venema

Reputation: 19346

Ow com'on nobody ever heard of an array indexed by boolean?

const
  BOOL_TEXT: array[boolean] of string = ('False', 'True');
  YES_NO_TEXT: array[boolean] of string = ('No', 'Yes');
  ERROR_OR_WARNING_TEXT: array[boolean] of string = ('Warning', 'Error');

It is in fact what BoolToStr itself uses!

function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;
const
  cSimpleBoolStrs: array [boolean] of String = ('0', '-1');

Upvotes: 25

Ken White
Ken White

Reputation: 125708

For converting Boolean to string, there's BoolToStr, which has been around since at least Delphi 2007. You can use it in your last example like this:

TextVal := BoolToStr((fsBold in Can.Font.Style), True);

For going the other direction (string to Boolean), you'd have to do an actual function. Something like this should get you started:

function StringToBoolean(const Value: string): Boolean;
var
  TempStr: string;
begin
  TempStr := UpperCase(Value);
  Result := (TempStr = 'T') or 
            (TempStr = `TRUE`) or 
            (TempStr = 'Y');
end;

BoolVal := StringToBoolean('True');     // True
BoolVal := StringToBoolean('False');    // False
BoolVal := StringToBoolean('tRuE');     // True

Of course, this doesn't work if there's nonsense in Value, but...

Upvotes: 7

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24096

In the unit StrUtils, there is ifthen()

StrVal := IfThen(BoolVal,'True','False');

And for this specific case you could even use:

StrVal := BoolToStr(BoolVal);

Upvotes: 46

Related Questions