Recent

Author Topic: [SOLVED] Is this a valid code?  (Read 461 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1712
[SOLVED] Is this a valid code?
« 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.  
« Last Edit: April 12, 2025, 12:48:10 pm by egsuh »

Paolo

  • Hero Member
  • *****
  • Posts: 643
Re: Is this a valid code?
« Reply #1 on: April 12, 2025, 12:17:50 pm »
Trystrtobool my help here ?

n7800

  • Hero Member
  • *****
  • Posts: 586
  • Lazarus IDE contributor
    • GitLab profile
Re: Is this a valid code?
« Reply #2 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.  

n7800

  • Hero Member
  • *****
  • Posts: 586
  • Lazarus IDE contributor
    • GitLab profile
Re: Is this a valid code?
« Reply #3 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.

egsuh

  • Hero Member
  • *****
  • Posts: 1712
Re: Is this a valid code?
« Reply #4 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