Lazarus

Programming => General => Topic started by: P.curtis on April 11, 2020, 11:05:33 am

Title: Boolean not working correctly
Post by: P.curtis on April 11, 2020, 11:05:33 am
The following does not, IMHO, work correctly, or am I doing it wrong?

Code: Pascal  [Select][+][-]
  1.   if bENABLED = true then
  2.     begin
  3.       ToggleBox1.Checked:=true;
  4.       ToggleBox1.Caption:='Enabled';
  5.       MyTray.Icon.LoadFromFile('phelper_en.ico') ;
  6.     end
  7.   else
  8.     begin
  9.       ToggleBox1.Checked:=false;
  10.       ToggleBox1.Caption:='Disabled';
  11.       MyTray.Icon.LoadFromFile('phelper_dis.ico') ;
  12.     end;
  13.  

Even if bENABLED = false then the first section gets executed.

What am I doing wrong?
Title: Re: Boolean not working correctly
Post by: P.curtis on April 11, 2020, 11:38:25 am
My mistake. Forget it.
Title: Re: Boolean not working correctly
Post by: ASerge on April 11, 2020, 12:46:14 pm
The following does not, IMHO, work correctly, or am I doing it wrong?

Code: Pascal  [Select][+][-]
  1.   if bENABLED = true then
  2.     begin
  3.       ToggleBox1.Checked:=true;
  4.       ToggleBox1.Caption:='Enabled';
  5.       MyTray.Icon.LoadFromFile('phelper_en.ico') ;
  6.     end
  7.   else
  8.     begin
  9.       ToggleBox1.Checked:=false;
  10.       ToggleBox1.Caption:='Disabled';
  11.       MyTray.Icon.LoadFromFile('phelper_dis.ico') ;
  12.     end;
  13.  
Better:
Code: Pascal  [Select][+][-]
  1. ToggleBox1.Checked := bENABLED;
  2. ToggleBox1.Caption := BoolToStr(bENABLED, 'Enabled', 'Disabled');
  3. MyTray.Icon.LoadFromFile(BoolToStr(bENABLED, 'phelper_en.ico', 'phelper_dis.ico'));
Title: Re: Boolean not working correctly
Post by: PascalDragon on April 11, 2020, 02:11:22 pm
In addition to what ASerge wrote you should not compare against = True or = False. Simply do

Code: Pascal  [Select][+][-]
  1. if bENABLED then
  2.   ...
  3.  
  4. // or
  5.  
  6. if not bENABLED then
  7.   ...

This will work for any expression returning a Boolean type.
TinyPortal © 2005-2018