IAMK Posted November 15, 2018 Posted November 15, 2018 In c++, I can use :: to refer to a variable with the same name 1 level up. How do I do this in AutoIT? Example code: Script.au3: #include "Function.au3" Global $number = 0 incrementNumber(5) Function.au3: Func incrementNumber($number) ::$number = $number + 1 EndFunc Thank you in advance.
kaisies Posted November 16, 2018 Posted November 16, 2018 (edited) Global means any function can directly access. Variables as parameters are local only. You can use byref like func(byref $v) to refer to a variable with its local name, but effect the global value. Edited November 16, 2018 by kaisies
IAMK Posted November 16, 2018 Author Posted November 16, 2018 @kaisies That won't work here. 1- There is a global variable and a local variable with the same name. 2- I do not want to pass the variable ByRef. Think of it this way: You have a set of buttons you can press. 1, 2, 3, 4 and 5. You use PressNextButton(ByRef $buttonNum) to press button 1, then increment and press button 2, then 3, etc... You also can pass any number you want to e.g. PressButton(4) which will press button 4, but then when you use PressNextButton(ByRef $buttonNum) it will continue from the last button you pressed using PressNextButton(). Think of it as a script where you can press any button you want by using PressButton($num), but at the end of the day, you need to use PressNextButton(ByRef $num) to navigate through all the buttons. Note: I do not wish to change any parameters or variables. I just want to make it explicit that I am scoping the global variable over the local variable.
BrewManNH Posted November 16, 2018 Posted November 16, 2018 11 hours ago, IAMK said: I just want to make it explicit that I am scoping the global variable over the local variable. That's not possible in AutoIt. If you have a global variable and a local variable with the same name and act on the variable name, you're acting on the local version of it. Global $Variable1 = 1 Test($Variable1) ConsoleWrite("$Variable1 = " & $Variable1 & " outside the function" & @CRLF) Func Test($Variable1) $Variable1 += 1 ConsoleWrite("$Variable1 = " & $Variable1 & " inside the function" & @CRLF) EndFunc ;==>Test If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
IAMK Posted November 16, 2018 Author Posted November 16, 2018 (edited) @BrewManNH Well, that's lame. Guess I have to start naming my variables differently in function files and pass an extra variable into my function. Thanks. Edited November 16, 2018 by IAMK
Earthshine Posted November 16, 2018 Posted November 16, 2018 Well this isn’t c++ TonyC? My resources are limited. You must ask the right questions
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