Jump to content
Sign in to follow this  
JohnBailey

Assign and IsDeclared - Problem

Recommended Posts

JohnBailey

The below code is an example of what I'm trying to do (note: the random function is just there for this example). Why does it say the variable does not exist when the Assign passes?

For $i = 0 to 2
    If Not Assign('htmlCaptureTab'&$i,Random(0,50,1)) Then 
        MsgBox(0,'Var not created','At position '&$i)
    Else
        If IsDeclared(Eval('htmlCaptureTab'&$i)) Then
            ConsoleWrite('Variable Exists : '&'htmlCaptureTab'&$i &@LF)
        Else
            ConsoleWrite('Variable does NOT exist : '&'$htmlCaptureTab'&$i &@LF)
        EndIf
    EndIf
Next

A decision is a powerful thing

Share this post


Link to post
Share on other sites
PsaltyDS

The below code is an example of what I'm trying to do (note: the random function is just there for this example). Why does it say the variable does not exist when the Assign passes?

For $i = 0 to 2
    If Not Assign('htmlCaptureTab'&$i,Random(0,50,1)) Then 
        MsgBox(0,'Var not created','At position '&$i)
    Else
        If IsDeclared(Eval('htmlCaptureTab'&$i)) Then
            ConsoleWrite('Variable Exists : '&'htmlCaptureTab'&$i &@LF)
        Else
            ConsoleWrite('Variable does NOT exist : '&'$htmlCaptureTab'&$i &@LF)
        EndIf
    EndIf
Next
Because you used Eval() in the wrong place:

For $i = 0 To 2
    If Not Assign('htmlCaptureTab' & $i, Random(0, 50, 1)) Then
        MsgBox(0, 'Var not created', 'At position ' & $i)
    Else
        If IsDeclared('htmlCaptureTab' & $i) Then
            ConsoleWrite('Variable Exists : ' & 'htmlCaptureTab' & $i & " = " & Eval('htmlCaptureTab' & $i) & @LF)
        Else
            ConsoleWrite('Variable does NOT exist : ' & '$htmlCaptureTab' & $i & " = " & Eval('htmlCaptureTab' & $i) & @LF)
        EndIf
    EndIf
Next

IsDeclared() wants the string name of the variable, not the contents of the variable.

P.S. This doesn't address the fact that Assign() and Eval() are evil, and you should be doing this with an array instead...

:)


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
JohnBailey

Because you used Eval() in the wrong place:

For $i = 0 To 2
    If Not Assign('htmlCaptureTab' & $i, Random(0, 50, 1)) Then
        MsgBox(0, 'Var not created', 'At position ' & $i)
    Else
        If IsDeclared('htmlCaptureTab' & $i) Then
            ConsoleWrite('Variable Exists : ' & 'htmlCaptureTab' & $i & " = " & Eval('htmlCaptureTab' & $i) & @LF)
        Else
            ConsoleWrite('Variable does NOT exist : ' & '$htmlCaptureTab' & $i & " = " & Eval('htmlCaptureTab' & $i) & @LF)
        EndIf
    EndIf
Next

IsDeclared() wants the string name of the variable, not the contents of the variable.

P.S. This doesn't address the fact that Assign() and Eval() are evil, and you should be doing this with an array instead...

:)

HAHA thank you PsaltyDS! I just discovered that about IsDelcared() - it needing a string name.

Good point about the array! :"> My actually code is MUCH longer and this was a VERY boiled down version. I was using Assign to store the variable id of a GUICtrl. However, as you said, I can do this with an array so THANK YOU and I'm a moron


A decision is a powerful thing

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  

×