How is double deref managed in v2?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

How is double deref managed in v2?

20 Feb 2024, 21:00

I had my mind wrapped around v1 so easily.

Code: Select all

Loop 5
var%A_Index%:=5*A_Index
MsgBox % var1 "`n" var2 "`n" var3 "`n" var4 "`n" var5
Great, we see the values 5, 10, 15, 20, 25.

But I'm trying to adapt this situation into a GUI on v2 and it's not liking it

Code: Select all

#Requires Autohotkey v2
#SingleInstance

global increment
increment:=1
myGui:=Gui("","Test")
additionGui:=myGui.Add("Button","","+")
additionGui.OnEvent("Click",AddMoreControls)
levelTitle:=myGui.Add("Text","x0","Placeholder")
myGui.Add("Edit","x0 w40 vExample%increment%","")
myGui.Show() ; Gui, Main:Show
return

AddMoreControls(*){
global increment
increment:=increment+1
myGui.Add("Edit","x0 w40 vExample%increment%","")
myGui.Show("Autosize") ; Gui, Main:Show, Autosize
}

#r::reload
And the error message:
Error: A control with this name already exists.

Specifically: Example%increment%

015: {
017: increment := increment+1
▶ 018: myGui.Add("Edit","x0 w40 vExample%increment%","")
019: myGui.Show("Autosize")
020: }
This makes me think that now it's being read as truly literal "vExample%increment%" with percent signs in name, that it's not getting vExample1 on the initial control and each press of the button it's not giving it vLevel2 or vLevel3, but increment is indeed increasing.

It's not clicking for me on what I should do for the & operator. Do I need to set up thousands of manual Example1 through Example9999 or whenever I get bored of typing or running a script to try typing it out for me and then somehow reference using the & to those?? It gets more complicated trying to shove these into the AHK v2 GUIs. Was easily done on v1 for me, but I'm trying to my best to adapt to v2 and use its new functionality. It's probably really simple, but if I can get just an example I can make heads or tails of it. Thanks!
teadrinker
Posts: 4335
Joined: 29 Mar 2015, 09:41
Contact:

Re: How is double deref managed in v2?

20 Feb 2024, 21:25

Your question has nothing to do with dereferencing or double dereferencing. Moreover, it is in fact not directly related even to v2. AHK v1 has the concept of expression, and you only need to understand how variables work in expressions. This is arranged the same way in both versions.

Code: Select all

AddMoreControls(*) {
    global increment
    myGui.Add("Edit", "x0 w40 vExample" . ++increment)
    myGui.Show("Autosize") ; Gui, Main:Show, Autosize
}
User avatar
mikeyww
Posts: 26979
Joined: 09 Sep 2014, 18:38

Re: How is double deref managed in v2?

20 Feb 2024, 21:27

Hello,

1. See the docs. https://www.autohotkey.com/docs/v2/Variables.htm#deref
2. Yep, it's literal, so use expressions instead here. It's easy with a bit of practice.

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
Loop 2
 g.AddEdit 'w230 vv' A_Index
g.Show
EDIT: oops, well, same as teadrinker here, but my description is less clear!

Although the script below works, it uses v1's "literal parameters", not expressions-- so this specific approach would not be used the same way in v2.

Code: Select all

#Requires AutoHotkey v1.1.33
Loop 2
 Gui Add, Edit, w230 vv%A_Index%
Gui Show
Finally, per teadrinker's note:

Code: Select all

#Requires AutoHotkey v1.1.33
Loop 2
 Gui Add, Edit, % "w230 vv" A_Index ; Expressions
Gui Show

Code: Select all

#Requires AutoHotkey v2.0
g := Gui(, 'Test')
Loop 3
 addMoreControls
g.Show

addMoreControls() {
 Static n := 0
 g.AddEdit 'w240 vv' (++n)
}

Code: Select all

#Requires AutoHotkey v2.0
g := Gui(, 'Test')
addMoreControls(3)
addMoreControls(2)
addMoreControls
g.Show

addMoreControls(n := 1) {
 Static i := 0
 Loop n
  g.AddEdit 'w240 vv' (++i)
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 48 guests