Jump to content

Can anyone tell me why this test code not work?


Guest
 Share

Recommended Posts

I think I found a logical bug in the compiler.

Because logically, the code I'm going to show you must Exit from the loop if $a > 5 but in fact, it is not happening...

What actually happens is this:

At the first round in the the main loop, the loop calling to the function Test($a) with $a = 1.

after that the return from Test($a) is 0.

then for some reason, the condition:

If $Test = "ExitLoop" Then

activated even if $Test is 0.

why this is happen?

i have autoit 3.3.8 not beta.

this is the code:

#AutoIt3Wrapper_Run_AU3Check=n
$a = 0
While 1
    Sleep(1000)
    ToolTip("test")
    $a = $a+1
    $Test = Test($a)
    MsgBox(0,"Return from Test($a):",$Test)
    If $Test = "ExitLoop" Then
        MsgBox(0,"","ExitLoop Executed",2)
        ExitLoop
    EndIf
WEnd

Func Test($a)
    If $a > 5 Then Return = "ExitLoop"
EndFunc
Edited by Guest
Link to comment
Share on other sites

WAD.

If $a not > 5 then the default return value of 0 (Integer) is being returned by function Test.

You then compare an Integer to a String. AutoIt internally converts the String to a number. Converting letters to a number gives 0. That's why the comparison is always True.

When your function always returns a string value then you are fine. That's good coding practice to always return the same type of data.

#AutoIt3Wrapper_Run_AU3Check=n
$a = 0
While 1
    Sleep(1000)
    ToolTip("test")
    $a = $a + 1
    $Test = Test($a)
    ConsoleWrite($Test & @LF)
    MsgBox(0, "Return from Test($a):", $Test)
    If $Test = "ExitLoop" Then
        MsgBox(0, "", "ExitLoop Executed", 2)
        ExitLoop
    EndIf
WEnd

Func Test($a)
    If $a > 5 Then Return "ExitLoop" ; <== Modified
    Return "" ; <== Added
EndFunc   ;==>Test
Edited by water

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

  • Moderators

gil900,

Firstly, you have been here long enough to know that you do NOT post general quesris in the Examples section - please pay more attention in future. ;)

And there is no bug in AutoIt, but several errors in your code.

 

- 1. ou have the wrong syntax for the Return line - you do not need the "=", as clearly shown in the Help file - and so you are returning a numreical 0 from each pass.

- 2. In the comparison you are therefroe comparing a number to a string (0 to "ExitLoop") - in this case Autoit converts the string to number and, guess what, it comes back as numerical 0 because there are no leading digits. So your comparison is true every time.

Take a look at this modified version:

#AutoIt3Wrapper_Run_AU3Check=n
$a = 0
While 1
    Sleep(500)
    $a = $a + 1
    $Test = Test($a)
    MsgBox(0, "Return from Test($a):", $Test)
    If $Test = "ExitLoop" Then
        MsgBox(0, "", "ExitLoop Executed", 2)
        ExitLoop
    EndIf
WEnd

Func Test($a)
    ; Always return a string
    Local $sRet = "Stay running" ; You could just use an empty string ""
    If $a > 5 Then
        $sRet = "ExitLoop"
    EndIf
    Return $sRet
EndFunc   ;==>Test
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

 

Firstly, you have been here long enough to know that you do NOT post general quesris in the Examples section - please pay more attention in future. ;)

Sorry, I did not mean to open it in the Examples section..

i even not noticed that I opened this thread in the Examples section.

 
Thank you for your help.

In fact I always write this method naturally:

Func Test($a)
    ; Always return a string
    Local $sRet = "Stay running" ; You could just use an empty string ""
    If $a > 5 Then
        $sRet = "ExitLoop"
    EndIf
    Return $sRet
EndFunc   ;==>Test

But until now I did not know how important it is ..

Now I know.

Thank you very much!

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