Sign in to follow this  
Followers 0
enaiman

Assign - behaviour

5 posts in this topic

I had a look at Assign command and I tried to figure out how to use it.

I will quote the help file here:

Assign

--------------------------------------------------------------------------------

Assigns a variable by name with the data.

Assign ( "varname", "data" [, flag] )

Parameters

varname The name of the variable you wish to assign. Cannot be an array element.

data The data you wish to assign to the variable.

flag [optional] controls the way that variables are assigned (add required options together):

0 = (default) Create variable if required

1 = Force creation in local scope

2 = Force creation in global scope

4 = Fail if variable does not already exist

Return Value

Success: Returns 1.

Failure: Returns 0 if unable to create/assign the variable.

In my understanding, if a variable is not declared yet then Assign has to create that variable and assign it a value.

I tried the following script:

MsgBox(0, "", Assign("variable", "Hello"));returns 1 = success
MsgBox(0, "var", $variable)

You can see that Assign is returning 1 so the creation and assign were successful but yet $variable is unknown.

How can this behaviour be explained?

Thanks,


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites



I had a look at Assign command and I tried to figure out how to use it.

I will quote the help file here:

In my understanding, if a variable is not declared yet then Assign has to create that variable and assign it a value.

I tried the following script:

MsgBox(0, "", Assign("variable", "Hello"));returns 1 = success
MsgBox(0, "var", $variable)

You can see that Assign is returning 1 so the creation and assign were successful but yet $variable is unknown.

How can this behaviour be explained?

Thanks,

Just ignore the warning you get from syntax check, and you'll see it actually does work. To avoid the syntax warnings you must use Eval() everywhere you use a variable that is DECLARED (not just assigned a value) with Assign():
Global $sString; Declares $sString
Assign ("sString", "Testing"); Assigns $sString = "Testing"
MsgBox(0, "$sString", $sString)

Assign("variable", "Hello"); Both declares $variable and assigns value
MsgBox(0, "$variable", Eval("variable")); Use Eval() because it was declared with Assign()

But... Assign and Eval are only slightly less evil than GOTO, so don't look for places to use them!

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

the answer, though not explained is in the help file

if you click 'Continue anyway' in SciTE then it will work with the second message box

it's the AutoIt wrapper complaining about a variable not being declared

Edit: quotes on SciTE button text

Remarks

If there is a need to use Assign() to create/write to a variable,

then in most situations, Eval() should be used to read the variable

and IsDeclared() should be used to check that the variable exists.

If Not IsDeclared("variable") Then Assign("variable", "Hello", 2)
MsgBox(0, "var", Eval("variable"))
;MsgBox(0, "var", $variable)
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

i tried this and yes it works... but it did gave error about variable not declared..

just click "Continue Anyway" and it works..

Share this post


Link to post
Share on other sites

Thanks PsaltyDS and rover

My bad, I haven't ignored the warning and I didn't click "continue anyway". This might come in very handy in the future.

Also it might provide the answer for http://www.autoitscript.com/forum/index.php?showtopic=82264

Thanks again :P


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0