Sign in to follow this  
Followers 0
laurin1

Comparative Statements...False is not false?

20 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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?


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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?

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


Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

In other words, I add this

If $bSharedINIFile == False Then
   
   $bSharedINIFile = False
   
  EndIf

The If evaluates to false, but otherwie, it evaluates to true.....is that a bug?


Keith Davis

MCSA, ZCE, A+, N+

http://www.laurinkeithdavis.com

Share this post


Link to post
Share on other sites

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


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

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

Here's my fix. just add it after the ini read.


[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
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
Sign in to follow this  
Followers 0