Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1403
  • Last Modified:

BoolToStr causes Undeclared Identifier error

I need to convert a boolean value to a string, but BoolToStr does not work. Why?

I'm using Delphi 5.0

Thanks,

J
0
JonathanGSK
Asked:
JonathanGSK
  • 3
  • 2
  • 2
  • +3
1 Solution
 
MerijnBSr. Software EngineerCommented:
that is strange, BoolToStr is not really something difficult,

can you show some code?
0
 
JohnjcesCommented:
I refreshed! :)

Delphi 5 does not have BoolToStr.

Here is a code snippet from Koders Code Search...

function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;
const
  cSimpleBoolStrs: array [boolean] of String = ('0', '-1');
begin
  if UseBoolStrs then
  begin
    VerifyBoolStrArray;
    if B then
      Result := TrueBoolStrs[0]
    else
      Result := FalseBoolStrs[0];
  end
  else
    Result := cSimpleBoolStrs[B];
end;

Their URL is:

http://www.koders.com/delphi/fid53147BF1A84109076F2982F8935A5A188DE37B0C.aspx

John
0
 
MerijnBSr. Software EngineerCommented:
and I didn't read the question thoroughly enough...

Johnjces posst looks good, I just wonder if TrueBoolStrs and FalseBoolStrs are available in D5?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
JohnjcesCommented:
MerijnB

I could not find it anywhere in my D5 install.

All of that is declared/defined in the  PXDelphi5.pas posted by Koders Code Search... same URL as above.

John
0
 
imitchieCommented:
is it not easier to write your own?
function BoolToStr(B: Boolean): string;
const
  cTrueFalseString: array [boolean] of String = ('true', 'false'); // change this
begin
  Result := cSimpleBoolStrs[B];
end;

Open in new window

0
 
ZhaawZSoftware DeveloperCommented:
imitchie, I think it should be  "(false, true)" :)

And I'd advise to use "if b then result := cTrueFalseString[true] else result := cTrueFalseString[false]", otherwise there could be situations when your function won't work:

var
    b : boolean;
begin
b := boolean(2);
ShowMessage(BoolToStr(b)); // your function; crashes
ShowMessage(SysUtils.BoolToStr(b, true)); // default D6's function; shows "true"
end;

"2" in "b := boolean(2)" could be result of some function which is not written by you, so such situations are possible.
0
 
aikimarkCommented:
somethings missing or altered from Koders snippet:

{ StrToBool converts the given string to a boolean value.  If the string
  doesn't contain a valid value, an EConvertError exception is raised.
  BoolToStr converts boolean to a string value that in turn can be converted
  back into a boolean.  BoolToStr will always pick the first element of
  the TrueStrs/FalseStrs arrays. }

var
  TrueBoolStrs: array of String;
  FalseBoolStrs: array of String;

const
  DefaultTrueBoolStr = 'True';   // DO NOT LOCALIZE
  DefaultFalseBoolStr = 'False'; // DO NOT LOCALIZE

function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;

procedure VerifyBoolStrArray;
begin
  if Length(TrueBoolStrs) = 0 then
  begin
    SetLength(TrueBoolStrs, 1);
    TrueBoolStrs[0] := DefaultTrueBoolStr;
  end;
  if Length(FalseBoolStrs) = 0 then
  begin
    SetLength(FalseBoolStrs, 1);
    FalseBoolStrs[0] := DefaultFalseBoolStr;
  end;
end;

function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string;
const
  cSimpleBoolStrs: array [boolean] of String = ('0', '-1');
begin
  if UseBoolStrs then
  begin
    VerifyBoolStrArray;
    if B then
      Result := TrueBoolStrs[0]
    else
      Result := FalseBoolStrs[0];
  end
  else
    Result := cSimpleBoolStrs[B];
end;
0
 
imitchieCommented:
zhaawz, i learned something new today!  I have refined the function

i never knew
var b: boolean
b := Boolean(2) is valid!
function BoolToStr(B: Boolean): string;
const
  cTrueFalseString: array [boolean] of String = ('false', 'true'); // change this
begin
  if B then
    Result := cTrueFalseString[false]
  else
    Result := cTrueFalseString[true];
end;

Open in new window

0
 
imitchieCommented:
except.. it is true =0, false=1
cos i debugged it
function BoolToStr(B: Boolean): string;
const
  cTrueFalseString: array [boolean] of String = ('true', 'false'); // change this
begin
  if B then
    Result := cTrueFalseString[false]
  else
    Result := cTrueFalseString[true];
end;

Open in new window

0
 
ZhaawZSoftware DeveloperCommented:
"except.. it is true =0, false=1"

if 0, then false, otherwise true ;) it has been like that for many years.


>> begin
>>   if B then
>>     Result := cTrueFalseString[false]
>>   else
>>     Result := cTrueFalseString[true];
>> end;

why do you return false on "if b"? "if b" is the same as "if b = true", so if it's true, you're returning "false" string, but if it's false, you're returning "true" string.
0
 
aikimarkCommented:
In Delphi, False and True are an enumeration of a boolean set.  As such, they have implied numeric values that are indexes into their positions in the [False, True] set.
0
 
JonathanGSKAuthor Commented:
Thanks to everyone who contributed to a VERY thorough analysis of this problem. John's answer is correct - I guess it's time to upgrade to Delphi7!

I have been using a very inelegant line, which works:

if Flag=True then String1:='True' else String1:='False';

But thanks for the alternatives,

Jonathan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now