Recent

Author Topic: Boolean not working correctly  (Read 684 times)

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Boolean not working correctly
« 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?

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Re: Boolean not working correctly
« Reply #1 on: April 11, 2020, 11:38:25 am »
My mistake. Forget it.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Boolean not working correctly
« Reply #2 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'));

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Boolean not working correctly
« Reply #3 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