1. Home
  2. Computing & Technology
  3. Delphi Programming
Glossary|Tips/Tricks|FREE App/VCL|Essentials|Books|Link Back

Delphi Programming RTL Reference|By Category|Alphabetically|By Unit
 

StrToBoolDef

unit
SysUtils
category
type conversion routines

declaration
function StrToBoolDef(const s : string; const Default: Boolean): boolean;

description
Returns a boolean from a string value, on error returns "Default".

While trying to convert a string to a boolean, StrToBool first tries to convert the string into a number; if the number is 0 the function returns False, True otherwise.
If s cannot be converted to a number, True is returned if S equals any of the strings in the TrueBoolStrs array; like wise False is returned if S equals any of the strings in the FalseBoolStrs.

If conversion is not possible the function returns the value in parameter "Default".

Where FalseBoolStrs and TrueBoolStrs are global Delphi variables (dynamic string arrays) you can set and use when converting a boolean variable to string and back.
When FalseBoolStrs and TrueBoolStrs are not set, StrToBoolDef (by default) treats TrueBoolStrs as if the first string in the array is 'True'; treats FalseBoolStrs as if the first string in the array is 'False'.

example
var s, sT, sF, sError : string;

s      := 'NO'; 
sT     := '2003'; 
sF     := '0'; 
sError := 'Not convertible';

SetLength(TrueBoolStrs,1);
SetLength(FalseBoolStrs,1);

TrueBoolStrs[0]:='YES';
FalseBoolStrs[0]:='NO';

(*
StrToBoolDef(s,TRUE) == False
StrToBoolDef(sT,TRUE) == True
StrToBoolDef(sF,TRUE) == False
StrToBoolDef(sError,TRUE) == TRUE
*)

in real code

see also
IntToStr, SetLength, BoolToStr, StrToBool


 Free Delphi code snippet inside every Delphi Newsletter!
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
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

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

All rights reserved.