Jump to content


Photo

Global %variable%


  • Please log in to reply
7 replies to this topic

#1 skrommel

skrommel
  • Members
  • 193 posts

Posted 03 April 2006 - 04:23 PM

Why won't Global accept this?

Skrommel


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


#2 Chris

Chris
  • Administrators
  • 10727 posts

Posted 03 April 2006 - 04:39 PM

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.

#3 SKAN

SKAN
  • Administrators
  • 9063 posts

Posted 03 April 2006 - 04:47 PM

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 !!!

#4 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 03 April 2006 - 04:52 PM

Or pass the variable ByRef...

#5 skrommel

skrommel
  • Members
  • 193 posts

Posted 04 April 2006 - 10:10 AM

:) 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

#6 SKAN

SKAN
  • Administrators
  • 9063 posts

Posted 04 April 2006 - 10:54 AM

:) 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, :)

#7 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 04 April 2006 - 12:06 PM

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?

#8 SKAN

SKAN
  • Administrators
  • 9063 posts

Posted 04 April 2006 - 12:10 PM

It was Lazslo!..

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

Regards, :)