Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Global %variable%


  • Please log in to reply
7 replies to this topic
skrommel
  • Members
  • 193 posts
  • Last active: Jun 07 2010 08:30 AM
  • Joined: 30 Jul 2004
Why won't Global accept this?

Skrommel


Function(variable)
{
  Global %variable%
  %variable%:=1
}


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
It's a documented limitation that dynamic variable names can't be declared. This is because variables are created as either local or global before the script actually begins running, so the program must know their names ahead of time.

This restriction may be removed in a future release (it's already on the to-do list).

Thanks.

SKAN
  • Administrators
  • 9114 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Skrommel, :)

Why won't Global accept this?

Function(variable)
{
  Global %variable%
  %variable%:=1
}


I dont know ! :roll:

But please tell me whether this is a workaround?!

Function(VarName) 
{ 
  Local Var
  Variable:=Varname
  %Variable%=1
}

Regards, :)

Edit: I did not see Chris` post !!!

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Or pass the variable ByRef...

skrommel
  • Members
  • 193 posts
  • Last active: Jun 07 2010 08:30 AM
  • Joined: 30 Jul 2004
:) Thanks for the replies!

I was thinking of using it to build GUIs, which requires global variables, so I'm afraid Global %variable% is the only alternative.

Skrommel

SKAN
  • Administrators
  • 9114 posts
  • Last active:
  • Joined: 26 Dec 2005

:) I'm afraid Global %variable% is the only alternative.Skrommel


Dear Skrommel, :)

See the Truth!

The following is an Alternative:

Function("Hello")
MsgBox, %Hello% ; displays "1"

Return

Function(VarName) 
{ 
  Local Var 
  Variable:=Varname 
  %Variable%=1 
}

I have declared Var as Local, which means everything else becomes Global.

I'm not sure from whom I learnt it. Should be either Lazslo or PhiLho

Regards, :)

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I was thinking of using it to build GUIs, which requires global variables, so I'm afraid Global %variable% is the only alternative.

I doubt it. Can you post an example of code that doesn't work, showing evidence of this need?

SKAN
  • Administrators
  • 9114 posts
  • Last active:
  • Joined: 26 Dec 2005
It was Lazslo!..

See how a Global Array of Variables are being created within a function.
http://www.autohotke... ... 3084#53084

Regards, :)