Jump to content

Comparative Statements...False is not false?


Recommended Posts

Why in AutoIt does this not work? $bTest = False does not equal false, it equals true when tested as part of an If statement.

$bTest = False

If $bTest Then

MsgBox(0, '', 'WILL SHOW THIS BOX')

EndIf

It works in PHP and other scripting language (that are loosely typed, anyway.) For AutoIt, I have to do this to test for False

$bTest = False

If $bTest <> False Then

MsgBox(0, '', 'WILL NOT SHOW THIS BOX')

EndIf

Now, if I set $bTest to 0 it works...so, why doesn't False == 0

$bTest = 0

If $bTest Then

MsgBox(0, '', 'WILL NOT SHOW THIS BOX'))

EndIf

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

  • Developers

Why in AutoIt does this not work? $bTest = False does not equal false, it equals true when tested as part of an If statement.

$bTest = False

If $bTest Then

MsgBox(0, '', 'WILL SHOW THIS BOX')

EndIf

No msgbox is shown for me with this code.

...and a msgbox is shown for:

$bTest = True

If $bTest Then
   MsgBox(0, '', 'WILL SHOW THIS BOX')
EndIf

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Oh, never mind. I was testing 'False', not False. Doh!

aahh... you test with 'false' in your editor but you change that when you post the question...

Does that mean you are retyping the code when posting?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

No, I was pulling the value from an .ini file, which I realized after I posted this question.

k.. that would do it too :huh2:

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

k.. that would do it too :huh2:

Sorry for the confusion, but on further review I was incorrect about checking 'False', in fact it appears that I am checking False after all and it does not work.

Now, if I set $bTest to False ($bTest = False), it works, but in this case I'm doing this:

$bSharedINIFile = IniRead($MASTERINIFILE, $SCRIPTNAME, 'SharedINIFile', False)

And then if I do this

MsgBox(0, '', $bSharedINIFile == False)

I get True

However, in this statement, the If evaluates to true

If $bSharedINIFile Then
   
   If FileExists($SHAREDINIFILEPATH & '\' & $sIniFileNameToLookFor) Then

    $sIniFileName = $SHAREDINIFILEPATH & '\' & $sIniFileNameToLookFor

    Return True
    
   EndIf
  
  Else

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

  • Developers

Can you make a simple script to explain your issue because you are confusing me.

Also realise the following:

- IniRead() returns a string

- comparing with == means: Tests if two strings are equal.

Anyway... make a short and sweet script that we can run and demonstrates your issue/question.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The == operator forces a case sensitive string comparison, so the keyword false is converted to the string "False" before the comparison.

When reading boolean values from an ini-file, I use this function to convert the string:

Func _Bool($vVal)
    ; Author: ProgAndy
    If IsBool($vVal) Then Return $vVal = True
    If IsNumber($vVal) Or StringIsDigit($vVal) Or StringIsFloat($vVal) Then Return Number($vVal) <> 0
    Switch $vVal
        Case "y", "yes", "true", "ja", "wahr", "j"
            Return True
        Case Else
            Return False
    EndSwitch
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

The place your script is going wrong is with ReadIni(). ReadIni() always returns a string so you need to test the values as a string like this.

$bSharedINIFile = IniRead($MASTERINIFILE, $SCRIPTNAME, 'SharedINIFile', "False")

If $bSharedINIFile = "False" Then
    MsgBox(0, '', '$bSharedINIFile = "False"')
Endif

If $bSharedINIFile = "True" Then
    MsgBox(0, '', '$bSharedINIFile = "True"')
EndIf

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

The place your script is going wrong is with ReadIni(). ReadIni() always returns a string so you need to test the values as a string like this.

$bSharedINIFile = IniRead($MASTERINIFILE, $SCRIPTNAME, 'SharedINIFile', "False")

If $bSharedINIFile = "False" Then
    MsgBox(0, '', '$bSharedINIFile = "False"')
Endif

If $bSharedINIFile = "True" Then
    MsgBox(0, '', '$bSharedINIFile = "True"')
EndIf

Ok, that is what is throwing me...I realize that if I return an actual value from the INI file of "False", that will be a string, but I mine was failing on the default value of False. That was what was confusing me.

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

Strings are considered as true when they are not empty. (equal to if Stringlen($string) > 0 then) I don't know why, bit this is how it works.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Strings are considered as true when they are not empty. (equal to if Stringlen($string) > 0 then) I don't know why, bit this is how it works.

I get that part, as it's the same in PHP (I'm really a PHP developer.) In PHP this is true:

"a"

true

1

And this is false:

"0"

0

false

""

What I still seems strange to me is that IniRead CONVERTS False to "False". In PHP, it would convert false to "".

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

What I still seems strange to me is that IniRead CONVERTS False to "False". In PHP, it would convert false to "".

That is not strange at all. An ini-file contains only text (strings). So anything you read from an ini-file is a string.

IniWrite converts all variables to a string before writing. Since AutoIt converts the boolean >False< to the string "False", and boolean >True< to "True", this is written to the file.

PS: I don't think you have understood my last post completely. In AutoIt, even the string "0" equals true since its length is 1.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

That is not strange at all. An ini-file contains only text (strings). So anything you read from an ini-file is a string.

IniWrite converts all variables to a string before writing. Since AutoIt converts the boolean >False< to the string "False", and boolean >True< to "True", this is written to the file.

PS: I don't think you have understood my last post completely. In AutoIt, even the string "0" equals true since its length is 1.

Yes, that is strange.

I'm not talking about a value READ from the INI file, I'm talking about the default value:

$bSharedINIFile = IniRead($MASTERINIFILE, $SCRIPTNAME, 'SharedINIFile', False)

The False here is NOT a string, but AutoIt is converting it into a string.

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

The default value has to be the same whether it is read from the ini or returned from the parameter if the value is missing. Inis return only strings, so the default value must always be a string. I think this behaviour is just natural.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

The default value has to be the same whether it is read from the ini or returned from the parameter if the value is missing. Inis return only strings, so the default value must always be a string. I think this behaviour is just natural.

Natural to return a string, sure, but to convert False to "False", that is not natural to me.

Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Link to comment
Share on other sites

Taken from the help file:

Return Value

Success: Returns the requested key value.

Failure: Returns the default string if requested key not found.

Therefore False is converted to the string "False".

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...