Associated Control Variables

Share your ideas as to how the documentation can be improved.
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Associated Control Variables

13 Feb 2021, 01:14

Immediately after the letter V, specify the name of a global variable (or a ByRef local that points to a global, or [in v1.0.46.01+] a static variable)
The last two of the options in Variables would apply to control creation within functions, no? How about:
Immediately after the letter V, specify the name of a variable. If the variable is desired to be global, declare it so, and if the control is created within a function, an outside declaration must match the variable name. Additionally within functions, the options for the variable can extend to either a ByRef local that points to a declared global, or [in v1.0.46.01+] a static variable).
There is also a little gotcha with parameter precedence related:

Code: Select all

;global testMe
; Makes no difference in the function
; if testMe is declared as a parameter!
Gui, Add, Checkbox, vtestMe gTest, Test
Gui, show
return
Test:
; Update testMe
Gui, Submit, Nohide
CheckTest(dummy)
CheckTest(testMe)
CheckTestGlobal()
Return
CheckTest(testMe)
{
msgbox % (testMe)? testMe: " testMe not the parameter!"
}
CheckTestGlobal()
{
msgbox % (testMe)? testMe: " testMe not declared global!"
}
GUIClose:
ExitApp
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: Associated Control Variables

13 Feb 2021, 16:53

There is nothing wrong with the current wording. If you run the following script, you will see the error "A control's variable must be global or static."

Code: Select all

test()
test() {
    Gui, Add, CheckBox, vtest
    Gui, Submit
    MsgBox % test
}
So there are 3 approaches to overcome this issue.

1. Global declaration:

Code: Select all

test()
test() {
    global test
    Gui, Add, CheckBox, vtest
    Gui, Submit
    MsgBox % test
}
2. ByRef:

Code: Select all

test(test)
test(ByRef test) {
    Gui, Add, CheckBox, vtest
    Gui, Submit
    MsgBox % test
}
3. Static declaration:

Code: Select all

test()
test() {
    static test
    Gui, Add, CheckBox, vtest
    Gui, Submit
    MsgBox % test
}
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: Associated Control Variables

14 Feb 2021, 09:22

Is this any better?
Immediately after the letter V, specify the name of a variable. The options for the variable when the control is created within a function block are limited to a require an explicit declaration of either global, where an outside declaration must match the variable name, a ByRef local that points to a declared global, or [in v1.0.46.01+] a static variable.
Edit: This is a bit kinder for new folks who don't require function blocks for their first scripts, I guess.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: Associated Control Variables

19 Feb 2021, 00:57

On the subject of globals which are declared inside functions (plz move if not relevant to OP) the difference between that and static can be seen with a timer called in the function (edited):

Code: Select all

#SingleInstance Force
#NoEnv
#Warn
SetBatchLines -1



; Initiate GUI

foo()
return

foo()
{
global ProgBar = 0
global ProgText = 0
global Percent := 0
;static ProgBar = 0
;static ProgText = 0
;static Percent := 0

	; Initiate Progressbar
	if (!ProgBar)
	{
	Gui 1: Color, white
	Gui 1: -Border
	Gui, 1: Hide

	Gui 1: Add, Progress, x20 y20 w160 h18 cGreen vProgBar Border	; Progressbar with border and color light greeen
	Gui Font, c0x606060 s9 Normal, Verdana					; FontSize s9 and color dark grey
	Gui 1: Add, Text, x0 y22 w200 +Center vProgText BackgroundTrans	; Text with transparent background

	Gui, 1: Show, h100 w200, ProgramInfo
	SetTimer, __UpdateProgressBar, 200

	}
__UpdateProgressBar:

	Percent += 5
	;msgbox % Percent
	GuiControl, 1:, ProgBar, %Percent%			; ProgressBar!
	GuiControl, 1:, ProgText, %Percent%	; Infotext in the Progressbar
		if (Percent >= 100)
		{
		SetTimer, __UpdateProgressBar, Delete
		goto EndFunc
		}	
	return
EndFunc:
ExitApp
Return
}

Esc::
ExitApp

;8208 error
;8224 quest
;8256 info


To test the difference replace the global decls with static decls where there is nada activity. The documentation seems to imply that any global variable wants a reference external to the function. Obviously not required here. Is it worth amending the docs to accommodate for this special case?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Return to “Suggestions on Documentation Improvements”

Who is online

Users browsing this forum: No registered users and 5 guests