Solved

BoolToStr causes Undeclared Identifier error

Posted on 2007-11-16
12
1,226 Views
Last Modified: 2010-04-21
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
Comment
Question by:JonathanGSK
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20300259
that is strange, BoolToStr is not really something difficult,

can you show some code?
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 100 total points
ID: 20300489
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 20300517
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
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300601
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
 
LVL 25

Expert Comment

by:imitchie
ID: 20303994
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 20304174
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:aikimark
ID: 20306368
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
 
LVL 25

Expert Comment

by:imitchie
ID: 20306498
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
 
LVL 25

Expert Comment

by:imitchie
ID: 20306502
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 20306766
"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
 
LVL 45

Expert Comment

by:aikimark
ID: 20308045
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
 

Author Closing Comment

by:JonathanGSK
ID: 31409596
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now