Jump to content

Assign() giving errors (Not Declared etc)


Recommended Posts

Im using assign to call some variables and at runtime I am getting errors:

WARNING: $Reserve_1: possibly used before declaration.

ERROR: $Reserve_1: undeclared global variable.

The code below is how I assign the vars, and I used a 'IsDeclared' to verify that it was successful.

Im trying to clean/convert old code and I would like to get rid of these 60 errors and 60 warnings thanks.

For $i = 1 to 60
if not Assign ( "Reserve_"&$i, "",2) then
  MsgBox(0,"Error Assigning :","Reserve_"&$i)
else
  ;MsgBox(0,"Assigning :","Reserve_"&$i)
EndIf
Next
msgbox(0,'IsDeclared ( Reserve_58 )',IsDeclared ( 'Reserve_58' ))
Link to comment
Share on other sites

Au3Check is giving the error message because it has no way to tell that there's a valid variable called $Reserve_58, so it errors out. Here's a way to get around it using Eval.

For $i = 1 To 60
     If Not Assign("Reserve_" & $i, $I, 2) Then
          MsgBox(0, "Error Assigning :", "Reserve_" & $i)
     Else
          ;MsgBox(0,"Assigning :","Reserve_"&$i)
     EndIf
Next
MsgBox(0, 'IsDeclared ( Reserve_58 )', Eval('Reserve_58')) ;<<<<<<<<<<<<<<<<<<<<< No error generated this way.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I don't get any errors. The variables are declared. See below:

For $i = 1 to 60
if not Assign ( "Reserve_"&$i, $i,2) then
  MsgBox(0,"Error Assigning :","Reserve_"&$i)
else
  ;MsgBox(0,"Assigning :","Reserve_"&$i)
EndIf
Next
msgbox(0,'IsDeclared ( Reserve_58 )',Execute("$Reserve_58"))
Link to comment
Share on other sites

shane0000,

I appears that you are trying to assign a variable with no data. czardas's example works because he is assigning the value of $i to the variable being created.

kylomas

edit: additional info - assign issues a return code.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

kylomas, Yes I am using the same way I would use 'Dim $Reserve_58'. I read in data from a file later to assign the values to alot of diferent vars that are Dim'ed as above ('Dim $sSomeVar') aswell as the 60 $Reserve vars.

The code runs fine, it just generates alot of errors at runtime (in autoit console).

Thanks

Edited by Shane0000
Link to comment
Share on other sites

shane0000 (f.y.i),

I ran this code to verify that your syntax is correct.

local $ret
For $i = 1 to 60
 $ret = Assign ( "Reserve_"&$i, "",2)
 consolewrite($ret & " | " )
Next

"success" was returned for each assign

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Yeah I tried them both, And I understand that its just going to have to remain.

BrewmanNH stated that the compiler has noway to know that the vars are being declared in such away.

And I can see that now, the compiler cant pre run the code to assign and verify that the vars had been declared. Thats like Inception I think, :) a compiler within a compiler.

Link to comment
Share on other sites

That works,

This Errors out in console

local $ret
local $thisVar
For $i = 1 to 60
$ret = Assign ( "Reserve_"&$i, "",2)
consolewrite($ret & " | " )
Next
$thisVar = $Reserve50

Because the compiler has no way to know that later on, $reserve50 will actually be a valid Var.

It just sees that $reserve50 has been called, but not set by standard ways, dim/local/global.

Edited by Shane0000
Link to comment
Share on other sites

As the help file says under Assign, if you have to use Assign to create the variables, then it's best to use Eval and IsDeclared to access them later. You CAN access them directly, but you are going to get Au3Check error messages.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

×
×
  • Create New...