1. Technology
Send to a Friend via Email
Glossary|Tips/Tricks|FREE App/VCL|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?
See More About

©2014 About.com. All rights reserved.