Jump to content
Sign in to follow this  
Shane0000

Assign() giving errors (Not Declared etc)

Recommended Posts

Shane0000

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
Shane0000

Neither of these methods removed the error @ run time. Console area.

My intent was to lessen the clutter as there are some out of date funcs giving errors also. This alone is generating 120 error/warnings.

Thanks for the help.

Share this post


Link to post
Share on other sites
Shane0000

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

Share this post


Link to post
Share on other sites
kylomas

shane0000,

I'm doing some more testing. It looks like what you are doing should have worked. Did you try what BrewmanNH suggested?

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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
Shane0000

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.

Share this post


Link to post
Share on other sites
Shane0000

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

Share this post


Link to post
Share on other sites
kylomas

shane0000,

Yes, I see that but vaguely recall a compiler directive for just this case.

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

Share this post


Link to post
Share on other sites
Shane0000

Yeah, I think thats what I should do czardas. I wrote this about 5 years ago , I dunno why I did it like this hehe.

There is like 1400 lines of code and Im trying to go back and figure out what all does what again.

Thanks for the help and info

Share this post


Link to post
Share on other sites
BrewManNH

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

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  

  • Similar Content

    • Burgs
      By Burgs
      Hello,
        I'd like to be able to create variables 'on the fly' by reading in some data from an SQLite database.  Using this data i'd like to create variables.  The data would be separated by colons (":")...'colon delimited'.  I would perform a 'StringSplit' on the data to create arrays holding each colon separated value.  Ideally I could then use these strings to create variables from them...possible?
        I know it cannot be done just piecing a string together such as "var = "$_" & 'some string'"... 
        I think it might work using the 'Assign' command...and there is an 'Opt' option to 'ExpandVarStrings' which seems to indicate it is possible to include a "$" within a string...however the documentation is a bit spotty on that.
        Can anybody confirm that these might be the best practices to create variables dynamically?  ...Perhaps there is another method I'm not thinking of...?   I thank you in advance.  Regards.  
    • kartune
      By kartune
      Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.
      Here is an example:
       
      <autoit>
      Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ
      GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
      </autoit>
      Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.
      I've already tried
      <autoit>
      Assign($clear1, $SATJ)
      </autoit>
       
      any help is greatly appreciated!!! 
    • MrCheese
      By MrCheese
      Hey Guys,
       
      So, the functions work, where primarygui() accurately determines the evaluation of the status of the checkboxes - the msgbox picks this up.
       
      However, later on, when we re-enter a 'for $i = 0 to ubound($checkbox)' loop, then in the 'batchinitial' function it doesn't picked up that the status&$i = 1, so it jumps out, then within the While 1 loop, it exits the loop in the first row, again because the status& $i= 0
      The "assign" line within the primarygui funtion, is this only a local assignment? if so, how can I make it cross function?
       
      Thank in adv for your help
       
       
      Func excelsheetlist() $i = 0 Global $aWorkSheets = _Excel_SheetList($oWorkbook1) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_SheetList Example 1", "Error listing Worksheets." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;_ArrayDisplay($aWorkSheets, "Excel UDF: _Excel_SheetList Example 1") ;_ArrayDisplay($aWorkSheets, "Array") Global $iRows = UBound($aWorkSheets, $UBOUND_ROWS) ; Total number of rows. In this example it will be 10. Global $iCols = UBound($aWorkSheets, $UBOUND_COLUMNS) ; Total number of columns. In this example it will be 20. Global $iDimension = UBound($aWorkSheets, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. MsgBox($MB_SYSTEMMODAL, "", "The array is a " & $iDimension & " dimensional array with " & _ $iRows & " row(s) & " & $iCols & " column(s).") Dim $checkbox[$iRows] EndFunc ;==>excelsheetlist Func primarygui() ; Create a GUI with various controls. Local $hGUI = GUICreate("Script Controller", 300, ($iRows * 24)) ; Create a checkbox control. ;Local $idCheckbox = GUICtrlCreateCheckbox("Standard Checkbox", 10, 10, 185, 25) Local $Button2 = GUICtrlCreateButton("Close", 210, 200, 85, 25) Local $Button3 = GUICtrlCreateButton("Run", 210, 170, 85, 25) Local $Button1 = GUICtrlCreateButton("Discharge", 210, 140, 85, 25) For $i = 0 To UBound($checkbox) - 1 $checkbox[$i] = GUICtrlCreateCheckbox($aWorkSheets[$i][0], 8, 0 + ($i * 24)) ;, 81, 17) Next ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 $nMsg = GUIGetMsg() Select Case $nMsg = $GUI_EVENT_CLOSE Exit Case $nMsg = $Button2 ;Close Exit Case $nMsg = $Button1 MsgBox(0, "Discharge Button not configured", "Now Exiting") Exit Case $nMsg = $Button3 ;Run $fSelection = False For $i = 0 To UBound($checkbox) - 1 If BitAND(GUICtrlRead($checkbox[$i]), $GUI_CHECKED) Then $fSelection = True ExitLoop EndIf Next If $fSelection Then For $i = 0 To UBound($checkbox) - 1 Assign("status" & $i, GUICtrlRead($checkbox[$i])) Next $batchcount = 0 For $i = 0 To UBound($checkbox) - 1 If Eval("status" & $i) = 1 Then $batchcount = $batchcount + 1 ;Call ("o" & $i & "copy") ; if you want to call the functions directly, remove ; before the call and comment or delete the following DirCopy statement MsgBox(0, "Checking", "Checking that: " & $checkbox[$i] & " no, with title: " & $aWorkSheets[$i][0] & " was selected, Batch count: " & $batchcount) ; if you need only the DirCopy EndIf Next ExitLoop Else MsgBox(48, 'No Items Selected', 'You have not selected any Patients to Load, Please select from the list') EndIf EndSelect WEnd ; Delete the previous GUI and all controls. GUIDelete($hGUI) EndFunc ;==>primarygui Func _IsChecked($idControlID) Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED EndFunc ;==>_IsChecked Func batchinitial() If Eval("status" & $i) = 1 Then debugbox() $oWorkbook1.Sheets(1).Activate $bigloop = $bigloop + 1 $sheet = $aWorkSheets[$i][0] $oWorkbook1.Sheets($sheet).Activate debugbox() EndIf EndFunc ;==>batchinitial OpenExcel() excelsheetlist() primarygui() For $i = 0 To UBound($checkbox) - 1 batchinitial() While 1 If Eval("status" & $i) = 0 Then ExitLoop ;all the rest of my script loops etc WEnd Next  
    • aleeksunder
      By aleeksunder
      Hello!
      Can anyone explain what's going on here:
      ; Initializing an array Local $a = [ 0 ] ; Lately in code I want to assign new array to $a variable $a = [ 1 ] and got this error at compile time
      ==> Error in expression.: $a = [ 1 ] $a = ^ ERROR What is it and why?
      And I've mentioned that I can't pass "manual" array to function as parameter:
      a( [ 0 ] ) Func a( $a ) _ArrayDisplay( $a ) EndFunc still the same error
      ==> Error in expression.: a( [ 0 ] ) a( ^ ERROR  
    • algiuxas
      By algiuxas
      Hello,
      I need to assign data to array:
      Global $Test[3][4] $Array = "Test" SetArrayData($Array,"Hello world!",1,3) ConsoleWrite($Array[1][3]&@CRLF) Exit Func SetArrayData($Array,$Data,$1,$2) $Array[$1][$2] = $Data; <-- ??? EndFunc I need that somebody would help me with that function
×