Jump to content

False VS 0


Juggler
 Share

Recommended Posts

Hello!

Coming from PHP where function can return false or zero - and zero result is not false result.

For example:

div (a, b ) {

if (b == 0)

return false; // we can't use 0 as divider

else

return a/b;

}

so when you call ths function you may get 0 or get false - so you can distinguish integer 0 with boolean false

if ( div(0, 5) ) - this check will pass ok with zero result and if evaluates to true!

if ( div(5, 0) ) - this check will not pass ok, result is not determined and is not returned and if evaluates to false

Which is the best way to achieve the same functionality in autoit?

I would like to write something like this (simplified version with ommited logic):

func read_data()

if data_read_ok() then

return $data ;data could be 0 too!

else

return false ;couldn't read data

endfunc

and use it in the script

$result = read_data() ;$result could be 0

if $result = false then

msgbox 'we were not able to read data'

else

msgbox 'we read data' & result ;again result could be 0 - means value read was 0

endif

The only way I've found is that you have first to check if returned variable is boolean and then to check if it was false.

Like this:

if IsBool($result) = True and $result = false then ...

But it is too ugly...

p.s.: there can be even worse cases - where function can return 0 1 true false (4 separate values) which gives us at least 5 outcomes

like this:

switch ($result):

case 0: ...

case 1:...

case true: ...

case false: ...

default: ...

- and I can't figure out how to handle it in autoit without loosing code readability...

Edited by Juggler
Link to comment
Share on other sites

Hi,

Welcome to the autoit forum ! :)

You need to add a double == to compare exactly the content :

If _toto(0) == 0 Then ConsoleWrite("0" & @CrLf)
If _toto(0) == False Then ConsoleWrite("Not 0" & @CrLf)

If _toto(1) == False Then ConsoleWrite("False" & @CrLf)
If _toto(1) == 0 Then ConsoleWrite("Not False" & @CrLf)

Func _toto($i)
If $i = 0 Then Return 0
Return False
EndFunc

PS : You should return the -1 value or something else, then you won't have the problem.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

  • Moderators

Juggler,

I would suggest using @error. By default it is set to 0 but you can change this by using the SetError function. So I would do something like this:

Func Read_Data()
    ; Read your data - perhaps like this
    $vData = FileRead($sFileName)
    ; Check you got a valid response
    If @error Then
        Return SetError(1, 0, 0) ; Error is set - who cares about the actual return value
    Else
        Return $vData            ; Error is not set - return is value read
    EndIf
EndFunc

Now all you need to do is to check @error in the same way when you get the return from your function. All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

What if function could return string 'False' (read from file for example) and boolean false?

haha. Then you need to use VarGetType, or like you mentioned IsBool, IsString.

Autoit works like that, you don't need to define a var type, then it's easier to use.

Br, FireFox.

Link to comment
Share on other sites

Now all you need to do is to check @error in the same way when you get the return from your function. All clear? :)

This is awful. Imagine you have complex if like

if $res1 and $res2 and ($res3 or $res4)

will unfold to many unreadable ifs. But I got the idea.

haha. Then you need to use VarGetType, or like you mentioned IsBool, IsString.

Autoit works like that, you don't need to define a var type, then it's easier to use.

Have you seen mongoDB?

It could return:

0 as integer

'0' as string

'false' as string

false as boolean

null - if not defined

coz mongodb it is nonsql and nonrelational database

and also it can return false if it was an error. that's what I call 'haha' :idiot:

But in simple autoit cases it should be enough to check - if returned result isbool - there was an error, otherwise we got valid result like 'false' string.

One more question:

If function can return integer, string, boolean how do I define variable to get the result?

func tst()
if bla bla bla then
  return number('123')
else if bla bla bla
 return 'string var'
else
 return True ;boolean
endif
endfunc

will it work like this:

$res = tst() - first time we got integer

later in the code

.

.

.

$res = tst() - here we got result as string

or it throws an error about datatype mismatch?

Link to comment
Share on other sites

But we don't care about the var type because we know it, for example you call a function which makes an addition, why would you care about getting the var type? It's a number of course.

I don't see the purpose of returning different var type. (more than 2)

One more question:

If function can return integer, string, boolean how do I define variable to get the result?

The only missmatch error you can get is about arrays (index error), there is no define type; autoit automatically determine it.

$blBool = False ;bool
$sBool = "False" ;string
;etc

And you can convert the var type with String(), Number()...

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Thank you very much. It is always a little hard to switch programming languages and the way of thinking same algorithms for different languages.

I see there will be Chrome JS UDF automation - it could be just great.

Now I'm trying to use PixelChecksum - but it doesn't work very well because of different resolutions and fonts and cleartype settings and windows themes etc.

There is Sikuli project for such automations - where you have to rely on images anywhere on the screen. Is there UDF like that for autoit?

Have a look at demos to get the idea http://sikuli.org/demo.shtml

Something like pixelchecksum somewhere on the screen...

Edited by Juggler
Link to comment
Share on other sites

  • Moderators

Juggler,

This is awful

Really? I do not see it in that light. :huh:

Here is your first example as I would write it in AutoIt:

$vA = 5 ; Change these as required to test
$vB = 0

$nRet = _Div($vA, $vB)

If @error Then
    ; This is only for info - you could make this as simple/complicated as you want
    Switch @extended
        Case 1
            ConsoleWrite("Parameter 1 is not a number" & @CRLF)
        Case 2
            ConsoleWrite("Parameter 2 is not a number" & @CRLF)
        Case 3
            ConsoleWrite("Parameter 2 is zero" & @CRLF)
    EndSwitch
Else
    ConsoleWrite("Result: " & $vA & "/" & $vB & "=" & $nRet & @CRLF)
EndIf

Func _Div($vX, $vY)
    ; Errorcheck the parameters
    If Not IsNumber($vX) Then Return SetError(1, 1, 0)
    If Not IsNumber($vY) Then Return SetError(1, 2, 0)
    If $vY = 0 Then
        Return SetError(1, 3, 0)
    Else
        Return $vX / $vY
    EndIf
EndFunc   ;==>_Div

It is always a little hard to switch programming languages and the way of thinking same algorithms for different languages

I have just seen this and I quite agree. One really does need to get over the "but I used to do it this way" mentality - we see a lot of that here. ;)

As to searching images onscreen, there are a couple of examples on the forum. I do not use them so I suggest searching a bit. :)

M23

Edit: FireFox has linked to one for you.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I use the @error approach all the time. It's easy and reliable.

You call a function.

You check @error so you can see if the function returned valid data.

If the data is valid, go on and process it, else send an error message to the user.

BTW: Do you juggle?

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

I see there will be Chrome JS UDF automation - it could be just great.

Right now, I'm not sure if I will make it, and I will make another project (update of an existing one of mine) before.

Maybe

Br, FireFox.

Edited by FireFox
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...