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

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

TryStrToBool

unit
SysUtils
category
type conversion routines

declaration
function TryStrToBool(const s : string; out b : boolean): boolean;

description
Converts a string to a boolean value with a Boolean success code.

If S can be converted to a number, TryStrToBool sets Value to false if that number is 0, true otherwise. If S can’t be converted to a number, TryStrToBool returns true if S is any of the strings listed in TrueBoolStrs (or differs from one only by case) and false if it is any of the strings listed in FalseBoolStrs (or differs from one only by case). If S is not a number and not one of the strings listed in TrueBoolStrs or FalseBoolStrs, TryStrToBool returns false to indicate that the conversion failed. If the conversion is successful, TryStrToBool returns true.

Where FalseBoolStrs ('FALSE' by default) and TrueBoolStrs ('TRUE' by default) are global Delphi variables (dynamic string arrays) you can set and use when converting a boolean variable to string and back.

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

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

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

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

(*
TryStrToBool(s, b) == False ; b == False
TryStrToBool(sT, b) == True; b == True
TryStrToBool(sF, b) == True; b == False
TryStrToBool(sError, b) == False!!
*)

in real code

see also
StrToBool, SetLength, BoolToStr, StrToBoolDef


 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.