Invalid variable declaration ? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Invalid variable declaration ?

30 Oct 2023, 04:34

Hi, I'm still transitioning to V2 and now and then I run into thing I don't understand, like below example.
Why is Global Line++ throwing an error ? In this case ?
I declared the variable as global at the top of the script, so what causes the error ?

Code: Select all

#Requires AutoHotkey v2.0
Global Line := 1


^2::
{
    ;global Line                := Line+1    ; <<<< This works
    Global Line++             ; <<<<<< This throws the error [color=#FF0000]Invalid variable declaration[/color]
}
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Invalid variable declaration ?  Topic is solved

30 Oct 2023, 04:45

Hello,

My hunch is that Global must always be followed by a variable name alone, though it can then also be followed by other assignment operators separately.

Code: Select all

Global x += 1
I'm not sure whether this is considered a bug in AutoHotkey.
User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: Invalid variable declaration ?

30 Oct 2023, 05:06

According to the list of expression operators, ++ is not defined as an assignment operator as += is.
More About Locals and Globals wrote:Any assignment operator can be used
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Invalid variable declaration ?

30 Oct 2023, 05:23

boiler wrote:
30 Oct 2023, 05:06
According to the list of expression operators, ++ is not defined as an assignment operator as += is.
More About Locals and Globals wrote:Any assignment operator can be used
Oke thank you.

Excuse my ignorance, but when I replace ++ by += I get this error: Error: Expected a String but got a Func.

Code: Select all

#Requires AutoHotkey v2.0
Persistent

global Line := 1

^2::
{
    global Line+=
    MsgBox Line
}
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: Invalid variable declaration ?

30 Oct 2023, 05:26

As with all other assignment operators, that operator would be followed by an expression used for the assignment, such as 1. See my script for an example.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: MagEpub and 25 guests