mr-es335 Posted November 9, 2023 Posted November 9, 2023 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: expandcollapse popupFunc 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. 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! mr-es335 Sentinel Music Studios
Andreik Posted November 9, 2023 Posted November 9, 2023 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: 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).
mr-es335 Posted November 9, 2023 Author Posted November 9, 2023 (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 November 9, 2023 by mr-es335 mr-es335 Sentinel Music Studios
Moderators Melba23 Posted November 9, 2023 Moderators Posted November 9, 2023 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Moderators Melba23 Posted November 9, 2023 Moderators Posted November 9, 2023 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
mr-es335 Posted November 9, 2023 Author Posted November 9, 2023 Melba, Thanks for the reply...appreciated! I finally did check the Wiki...and observed the example noted! I gather I will be consulting the WIKi immediately after I have read the HelpFile! mr-es335 Sentinel Music Studios
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now