Opened 6 years ago
Closed 6 years ago
#3643 closed Bug (No Bug)
For...To...Step...Next - variable is NOT (see Remarks in AutoIt Help) in Local scope!
Reported by: | Bitnugger | Owned by: | |
---|---|---|---|
Milestone: | Component: | AutoIt | |
Version: | 3.3.14.5 | Severity: | None |
Keywords: | Cc: |
Description
#include <AutoItConstants.au3> Local $sScope, $sRemarks = ' --> Remarks: The variable will be created automatically with Local scope, even when MustDeclareVars is on.' ConsoleWrite('> For $i = 0 To 1 Step 1' & @CRLF) For $i = 0 To 1 Step 1 ConsoleWrite('@ $i = ' & $i & @CRLF) Next Switch IsDeclared('i') Case $DECLARED_GLOBAL ; 1 $sScope = '! $i = $DECLARED_GLOBAL ( 1) ' Case $DECLARED_LOCAL ; -1 $sScope = '+ $i = $DECLARED_LOCAL (-1) ' Case $DECLARED_UNKNOWN ; 0 $sScope = '- $i = $DECLARED_UNKNOWN ( 0) ' EndSwitch ConsoleWrite($sScope & $sRemarks & @CRLF)
Attachments (0)
Change History (4)
comment:1 Changed 6 years ago by Jos
comment:2 Changed 6 years ago by anonymous
Ok, better I would have said it's not an error ... but that the text under Remarks leads to assumption that $i would be in the Local scope anyway.
The trigger for my ticket was the Notify UDF by Melba23 ... line 47-49.
#include <Array.au3> #include <Notify.au3> ;~ ... ; Notify.au3 - line 46 For $i = 0 To UBound($aNotify_Settings, 2) - 1 ; $i = Global scope!!! $aNotify_Settings[1][$i] = $aNotify_Settings[0][$i] Next ;~ ... ConsoleWrite(_ArrayToString(_MyFunc(), ', ') & @CRLF) Func _MyFunc() Local $aRet[7] = [1, 2, 3, 4, 5, 6, 7] ;~ ... ; No error... funny, how can that? $i is not declared in this function! ;-) $aRet[$i] = 10000 ; <-- ;~ ... Return $aRet EndFunc
comment:3 Changed 6 years ago by Jos
This is how it is described in the Helpfile under: AutoIt/Language Reference/Variables and look for the paragraph Scope:
===========================================================================================
Scope
A variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the global scope and can be read or changed from anywhere in the script.
If you declare a variable inside a function it is in local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.
By default when variables are declared using Dim or assigned in a function they have local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.
See Static for details on how to use this keyword as part of the scope declaration.
============================================================================================
Hope that explains it.
Jos
comment:4 Changed 6 years ago by Jos
- Resolution set to No Bug
- Status changed from new to closed
Guidelines for posting comments:
- You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
- In-depth discussions should take place on the forum.
For more information see the full version of the ticket guidelines here.
Any variable in the main part of the script is global.
Try this version to see what i mean:
So this is as designed and no bug.
Jos