Jump to content
Sign in to follow this  
CreeTar

boolean comparions hits where it shouldnt

Recommended Posts

CreeTar

How come that this code:

$a = 0
$b = "empty to prevent"

ConsoleWrite($a=$b)

will return TRUE, according to the Help "=" is for in-casesensitive string comparison.

But obviously it fails here. Or can't i compare a number with a string?

The problem is cause by a GetMsg() which is 0, but to prevent a button from working

I just gave him a random string, still the comparison hits with TRUE.

Share this post


Link to post
Share on other sites
sahsanu

But obviously it fails here. Or can't i compare a number with a string?

The problem is cause by a GetMsg() which is 0, but to prevent a button from working

I just gave him a random string, still the comparison hits with TRUE.

Hello,

You can't (or at least you shouldn't ;-)) compare numbers and strings:

;Check this and you will never see the console write
$a = 0
$b = "empty to prevent"
If IsString($a) Then ConsoleWrite($a=$b)

;And here you will get a beautiful False
$a = ""
$b = "empty to prevent"
If IsString($a) Then ConsoleWrite($a=$b)

Cheers,

sahsanu

Share this post


Link to post
Share on other sites
CreeTar

Hello,

You can't (or at least you shouldn't ;-)) compare numbers and strings:

;Check this and you will never see the console write
$a = 0
$b = "empty to prevent"
If IsString($a) Then ConsoleWrite($a=$b)

;And here you will get a beautiful False
$a = ""
$b = "empty to prevent"
If IsString($a) Then ConsoleWrite($a=$b)

Cheers,

sahsanu

but on the other hand $a==$b returns false, how come?

since AutoIt is not very type-orientated and variables can't be delcared as int or string

this should not happen

Share this post


Link to post
Share on other sites
KaFu

#cs
Language Reference - Operators:
"= Tests if two values are equal."

Language Reference - Datatypes:
"If a string is used as a number, an implicit call to Number() function is done."
#ce

$a = 0
$b = "empty to prevent"

If $a = $b Then ; implicit call to Number($b) = 0
    ConsoleWrite("+ True" & @crlf)
Else
    ConsoleWrite("- False" & @crlf)
EndIf

If Number($a) = Number($b) Then
    ConsoleWrite("+ True" & @crlf)
Else
    ConsoleWrite("- False" & @crlf)
EndIf

If string($a) = string($b) Then
    ConsoleWrite("+ True" & @crlf)
Else
    ConsoleWrite("- False" & @crlf)
EndIf


#cs
Language Reference - Operators:
"== Tests if two strings are equal"
#ce
If $a == $b Then
    ConsoleWrite("+ True" & @crlf)
Else
    ConsoleWrite("- False" & @crlf)
EndIf

Share this post


Link to post
Share on other sites
CreeTar

cheers for the help, i initialize the variable now with -123 and hope that does it.

next time i do an event GUI and not that annoying polling stuff :mellow:

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  

×