Jump to content

boolean comparions hits where it shouldnt


Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

#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

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...