Lazarus

Programming => General => Topic started by: egsuh on April 12, 2025, 11:03:52 am

Title: [SOLVED] Is this a valid code?
Post by: egsuh on April 12, 2025, 11:03:52 am
In following example, if Options['LoopSelect'] is not 'true' or 'false' then EConvertError will be raised.
I'd like to treat '' as false.

Code: Pascal  [Select][+][-]
  1. function TBlockStart.getLoopSelect: boolean;
  2. begin
  3.    try
  4.       Result := StrToBool(Options['LoopSelect']);
  5.    except
  6.       Result := False;
  7.    end;
  8. end;
  9.  
Title: Re: Is this a valid code?
Post by: Paolo on April 12, 2025, 12:17:50 pm
Trystrtobool my help here ?
Title: Re: Is this a valid code?
Post by: n7800 on April 12, 2025, 12:23:22 pm
Actually, there is StrToBoolDef for this:
Code: Pascal  [Select][+][-]
  1. function StrToBoolDef(const S: string; Default: Boolean): Boolean;
  2.  
Title: Re: Is this a valid code?
Post by: n7800 on April 12, 2025, 12:25:46 pm
By the way, there are global arrays TrueBoolStrs and FalseBoolStrs, in which you can specify acceptable values.
Title: Re: Is this a valid code?
Post by: egsuh on April 12, 2025, 12:47:45 pm
   function StrToBoolDef(const S: string; Default: Boolean): Boolean;

This really helps, and good enough. Thank you very much.
TinyPortal © 2005-2018