Jump to content
Sign in to follow this  
MilesAhead

Why 0 or 1 as Boolean different from .ini file?

Recommended Posts

MilesAhead

I'm just wondering why I have to put If $val <> 0 Then

when reading values from an .ini file and I don't when in script?

Here's an example of 0 as False and 1 as True. When the value is set in script it works as expected. But when I read the value from an .ini file I have to specify If $val <> 0 Then

Just a bit strange. It's one of those things I tend to forget and bugs result. Is there a way I can force the read from Ini to be a Boolean? It's more intuitive to do If $val Then yadda yadda

Global $theParam = 0
WriteIniParam()
Sleep(100)
ReadIniParam()
If Not $theParam Then
MsgBox(0x1040, "Test", "Bool False")
Else
MsgBox(0x1040, "Test", "Bool True")
EndIf
 
If ReturnParam(1) Then
MsgBox(0x1040,"Test","Bool True")
EndIf
If Not ReturnParam(0) Then
MsgBox(0x1040,"Test","Bool False")
EndIf
 
Func ReturnParam($param)
Return $param
EndFunc
Func WriteIniParam()
IniWrite("Test.ini", "Settings", "UseParam", $theParam)
EndFunc
Func ReadIniParam()
$theParam = IniRead("Test.ini", "Settings", "UseParam", $theParam)
EndFunc
Edited by MilesAhead

Share this post


Link to post
Share on other sites
BrewManNH

Unless you read the ini file information using Number() then it gets read as a string. Strings are interpreted as being 0 or False when doing a boolean comparison. When you compare a string as if it's a number ($val <> 0) then the string is converted automatically using Number(), if the string contains is a number, "0" or "12" for example, then it's converted to that number. If the string doesn't contain just numbers, it equals 0.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
MilesAhead

Thanks for that explanation.

I hate If $x <> 0 so I'm messing around with _Iif() and Number()

I'll get it eventually.

Edit: this seems to produce the correct result. I just have to remember to add the _Iif()s and Number() funcs

#include <Misc.au3>
 
Global $theParam = True
WriteIniParam()
Sleep(100)
ReadIniParam()
 
If Not $theParam Then
  MsgBox(0x1040, "Test", "Bool False")
Else
  MsgBox(0x1040, "Test", "Bool True")
EndIf
 
 
If ReturnParam(1) Then
  MsgBox(0x1040, "Test", "Bool True")
EndIf
 
If Not ReturnParam(0) Then
  MsgBox(0x1040, "Test", "Bool False")
EndIf
 
 
Func ReturnParam($param)
  Return $param
EndFunc   ;==>ReturnParam
 
Func WriteIniParam()
  IniWrite("Test.ini", "Settings", "UseParam", _Iif($theParam, 1, 0))
EndFunc   ;==>WriteIniParam
 
Func ReadIniParam()
  $theParam = _Iif(Number(IniRead("Test.ini", "Settings", "UseParam", _Iif($theParam,1,0))),True,False)
EndFunc   ;==>ReadIniParam

Another Edit: also I do see what you mean now. If I want to use a variable with 0 for False and 1 for True I just have to wrap the IniRead with Number(). I can still use If Not $val etc..

Edited by MilesAhead

Share this post


Link to post
Share on other sites
Spiff59

Isn't this sufficient?

#include <Misc.au3>
Global $Param0, $Param1
WriteIniParam()
Sleep(100)
ReadIniParam()
If $Param0 Then
  MsgBox(0x1040, "Param0", "Fail!")
Else
  MsgBox(0x1040, "Param0", $Param0 &  " = Bool False")
EndIf
If $Param1 Then
  MsgBox(0x1040, "Param1", $Param1 &  " = Bool True")
Else
  MsgBox(0x1040, "Param1", "Fail!")
EndIf
Func WriteIniParam()
  IniWrite("Test.ini", "Settings", "Param0", "0")
  IniWrite("Test.ini", "Settings", "Param1", "1")
EndFunc   ;==>WriteIniParam
Func ReadIniParam()
  $Param0 = Number(IniRead("Test.ini", "Settings", "Param0", "x"))
  $Param1 = Number(IniRead("Test.ini", "Settings", "Param1", "x"))
EndFunc   ;==>ReadIniParam

Edit: Ah... maybe your edit means you're trashing all the _Iif's...

Edited by Spiff59

Share this post


Link to post
Share on other sites
MilesAhead

Right. If a variable with 0 is False and non 0 is True. The _Iif() business is only if I have some reason to insist the variable be either False or True rather than 0 or1. Don't know why it would have to be that way but I figured it out if I need it. :graduated:

Sometimes I look too much for the arcane stuff first. ;)

Share this post


Link to post
Share on other sites
Juvigy

So ? Just use :

If $val <> "0" then

Share this post


Link to post
Share on other sites
MilesAhead

I want to get away from the comparison and just treat it like a Bool. As in If $val then

Assigning it 1 or True will work as long as I use Number reading out of .ini file. It's like Registry settings. Default is to read as string.

I don't see that a string comparison does me any good.

Maybe someone familiar with the spec can verify but just trying with my example it seems assigning 1 or 0 to a variable and comparing to True or False works, so it may be a distinction without a difference.

Unless this is just a weird side-effect I'll take it as evidence I don't need to resort to _Iif() stuff reading .ini files. :graduated:

Global $bool = 0
 
If $bool = True Then
  MsgBox(0x1040,"Test", "Compares favorably with True")
Else
  MsgBox(0x1040,"Test", "Compares favorably with False")
EndIf

Likewise it still works as expected with just the bool syntax and no comparison

Global $bool = 1
 
 
If $bool Then
  MsgBox(0x1040,"Test", "Compares favorably with True")
Else
  MsgBox(0x1040,"Test", "Compares favorably with False")
EndIf

Hmmmm, if you are going to save settings to ini file then in a way it argues against ever assigning True or False since then you'd get Option=True in the ini file instead of 1 like you want.

Edited by MilesAhead

Share this post


Link to post
Share on other sites
BrewManNH

if you'd like to see how the language handles various data types then check out the help file. Look for the section titled "Language Reference - Datatypes" it explains a it lot better than I did.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
MilesAhead

Hmmmmmm, for sloppy programming perhaps I should use this technique for reading and writing Bools to ini

That way when the global $theParam is declared it doesn't really matter if I assign it 1 or True. It will pass all the Boolean tests and go in and out of ini files without causing bugs.

Func WriteIniParam()
  IniWrite("Test.ini", "Settings", "UseParam", _Iif($theParam, 1, 0))
EndFunc   ;==>WriteIniParam
 
Func ReadIniParam()
  Local $val = _Iif($theParam,1,0)
  $theParam = Number(IniRead("Test.ini", "Settings", "UseParam", $val))
EndFunc   ;==>ReadIniParam

Share this post


Link to post
Share on other sites
MilesAhead

This way of doing it should allow a bit more flexibility. User can set an option as "enabled" by using 1 or True. Whatever floats the boat.

; $val is a string read by IniRead that
; should indicate if an option is
; enabled or disabled. The user can
; set it to True or 1 for enabled.
; (e.g. UseMiddleMouseClick=True)
;
; returns True if $val is "True" or "1"
; otherwise returns False
;
Func _IniBool($val)
  If $val = "True" Or $val = "1" Then Return True
  Return False
EndFunc   ;==>_IniBool

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  

×