Jump to content

Recommended Posts

Posted

Good day,

I am, apparently, having some "issues" regarding variable "scope" that I would appreciate some assistance with.

According to the HelpFile,

Quote

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.

Well, this appear NOT to be the case here! here is the scenario:

1. I have 4 similar functions, namely:
• Func Bup_Type_Data(),Func Rst_Type_Data(), Func Backup_CT_Data() and Func Restore_CT_Data()
2. All that is different between these 4 functions are the source and destination locations where the data resides.
3. Within those 4 functions is a variable named $CopyFiles.
3. All variables then, should be local to that particular Function.

Here are the four scripts:

Func Bup_Type_Data()
    Func CopyFiles($sFileOpenDialog, $sDestination, $FC_OVERWRITE)
        If StringInStr($sFileOpenDialog, "|") Then
            Local $aFileSplit = StringSplit($sFileOpenDialog, "|")
                For $i = 2 To $aFileSplit[0]
                    FileCopy($aFileSplit[1] & "\" & $aFileSplit[$i], $sDestination, $FC_OVERWRITE)
                Next
        Else
            FileCopy($sFileOpenDialog, $sDestination, $FC_OVERWRITE)
        EndIf
    EndFunc
; -----------------------------------------------
Func Rst_Type_Data()
    Func CopyFiles($sFileOpenDialog, $sDestination)
        If StringInStr($sFileOpenDialog, "|") Then
            Local $aFileSplit = StringSplit($sFileOpenDialog, "|")
                For $i = 2 To $aFileSplit[0]
                    FileCopy($aFileSplit[1] & "\" & $aFileSplit[$i], $sDestination, $FC_OVERWRITE)
                Next
        Else
            FileCopy($sFileOpenDialog, $sDestination, $FC_OVERWRITE)
        EndIf
    EndFunc
; -----------------------------------------------
Func Backup_CT_Data()
    Func CopyFiles($sFileOpenDialog, $sDestination)
        If StringInStr($sFileOpenDialog, "|") Then
            Local $aFileSplit = StringSplit($sFileOpenDialog, "|")
                For $i = 2 To $aFileSplit[0]
                    FileCopy($aFileSplit[1] & "\" & $aFileSplit[$i], $sDestination, $FC_OVERWRITE)
                Next
        Else
            FileCopy($sFileOpenDialog, $sDestination, $FC_OVERWRITE)
        EndIf
    EndFunc
; -----------------------------------------------
Func Restore_CT_Data()
    Func CopyFiles1($sFileOpenDialog1, $sDestination)
        If StringInStr($sFileOpenDialog1, "|") Then
            Local $aFileSplit = StringSplit($sFileOpenDialog1, "|")
                For $i = 2 To $aFileSplit[0]
                    FileCopy($aFileSplit[1] & "\" & $aFileSplit[$i], $sDestination, $FC_OVERWRITE)
                Next
        Else
            FileCopy($sFileOpenDialog1, $sDestination, $FC_OVERWRITE)
        EndIf
    EndFunc
    ; -----------------
    Func CopyFiles2($sFileOpenDialog2, $sDestination)
        If StringInStr($sFileOpenDialog2, "|") Then
            Local $aFileSplit = StringSplit($sFileOpenDialog2, "|")
                For $i = 2 To $aFileSplit[0]
                    FileCopy($aFileSplit[1] & "\" & $aFileSplit[$i], $sDestination, $FC_OVERWRITE)
                Next
        Else
            FileCopy($sFileOpenDialog2, $sDestination, $FC_OVERWRITE)
        EndIf
    EndFunc
; -----------------------------------------------

Observations
1. As can be observed, Func Backup_CT_Data() has two $CopyFile variables that are numerically different - I receive a duplicate function name error if I do not.

duplicate_error.png.b598da5731ae1aa41393c08943668408.png

2. Each of the other 3 Functions employ the same $CopyFiles variables

Questions
1. Are these issues the result of NOT actually declaring the $CopyFile variable ahead of time?
2. If I employ the use of "MustDeclareVars", I apparently have quote a number of undeclared variables!

Any assistance would be appreciated. Thank you!

Posted

You really need to take your time to learn the basics of AutoIt. The code provided it's not valid AutoIt code that can run.

Func Bup_Type_Data()
    Func CopyFiles($sFileOpenDialog, $sDestination, $FC_OVERWRITE)

Such constructs of nested functions are not valid in AutoIt.

10 minutes ago, mr-es335 said:

duplicate_error.png.b598da5731ae1aa41393c08943668408.png

And this error doesn't have anything to do with the scope of variables. The error it's self explaining: you have a function (CopyFiles) with the same name within the same script (including the headers).

Posted (edited)

Andriek,

I should have noted that the above are taken from a lager work and have been provided to show the similarities within them. This is NOT how the code is actually laid out.

I hope this note will help to clarify things a bit?

Update!!: I just discovered this form the Wiki!!

Quote

Via the Tray icon

You can add this line at the top of your script (1 is on, 0 is off) to enable debugging from the tray icon. [Emphasis mine]

AutoItSetOption ("TrayIconDebug", 1);0-off

May I ask why this information is not in the HelpFile?

Edited by mr-es335
  • Moderators
Posted

mr-es335,

Quote

May I ask why this information is not in the HelpFile?

But it is - look under AutoItSetOption for the section TrayIconDebug.

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

 

  • Moderators
Posted

mr-es335,

Quote

having some "issues" regarding variable "scope"

May I suggest reading the Variables - using Global, Local, Static and ByRef tutorial in the Wiki.

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

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...