Sign in to follow this  
Followers 0
BlackShadow

Declaration variable errors when you use Assign()

7 posts in this topic

I'm using Assign() to create variables during runtime, but the compiler itself gives me the following warnings/errors:

WARNING: $version: possibly used before declaration.

ERROR: $version: undeclared global variable.

I tried using Eval() to create global declarations, but it doesnt seem to matter since AutoItSetOption("MustDeclareVars", 0) as default. (Even try adding that line just in case)

So I guess I either need to make the compiler ignore those errors, since the file do work if I Ignore the erros, or I need find some way to dynamically declare dynamic variables......

Share this post


Link to post
Share on other sites



@BlackShadow

You can aslo do :

$version = "data"

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

The point of using Assign() is that you are creating a variable with a string parameter representing the actual variable. This is unknown to the syntax checker and because you choose to use a string to represent a variable, then you should use a string representation to access the variable. This is where Eval() comes in to play to read the string representation of the variable.

A brief example to create a variable with Assign() and read with Eval() for display

Assign('version', 'data')
ConsoleWrite(Eval('version') & @CRLF)

:)

Share this post


Link to post
Share on other sites

Thanks for the replies, but I guess I need to make myself a bit more clear.

I have a table with 2 fields in it. One is the Variable Name, the other is the Variable Value.

_SQlite_Query (-1, "SELECT * FROM l_variables;", $hQuery); the query
While _SQLite_FetchData ($hQuery, $aRow) = $SQLITE_OK
    Assign($aRow[0], $aRow[1])
WEnd

Then I use those variables in my code late down, like here:

$ui_form = GUICreate("Programname " & $version, 801, 401, 202, 129)

And it's on that line I get the declaration error since $version have not been declared anywhere.

Doing this in the loop above does nothing, except perhaps feel better:

;   Eval("Global $" & $aRow[0])

And I do not want to manually declare my variables.

I could solve it by making an include file for my programs (the system consists of several small programs), but I prefer to have my "global" variables/constants done in a DB and then accessed from all subprograms.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The point of using Assign() is that you are creating a variable with a string parameter representing the actual variable. This is unknown to the syntax checker and because you choose to use a string to represent a variable, then you should use a string representation to access the variable. This is where Eval() comes in to play to read the string representation of the variable.

A brief example to create a variable with Assign() and read with Eval() for display

Assign('version', 'data')
ConsoleWrite(Eval('version') & @CRLF)

:)

I missed your post when I wrote my reply, but I guess this means it should work if I change my lines to something like:

$ui_form = GUICreate("Programname " & Eval('version')  , 801, 401, 202, 129)

Not pretty, but as long as it works. :o

Would prefer to just be able to toggle off the warnings from the compiler thou, and keep the code "cleaner".

Thanks for the help anyway!

Edited by BlackShadow

Share this post


Link to post
Share on other sites

Would prefer to just be able to toggle off the warnings from the compiler thou, and keep the code "cleaner".

Do not think you understand your mistake. I mentioned that declaring a variable as a string representation is dynamic as you mentioned and reading it should be as a string representation (also can be known as dynamic) should be done. The warnings are deserved as you were trying to access a variable created by a string by using a $variable (notice the $ representation) that should be declared as suitable with being a $variable if accessed that way.

2 methods shown

Assign('version', 'data')
$ui_form = GUICreate("Programname " & Eval('version')  , 801, 401, 202, 129)oÝ÷ Ù«­¢+Ù±½°ÀÌØíÙÉÍ¥½¸(ìÀÌØíÙÉÍ¥½¸ôÌäíÑÌäì(ÀÌØíÕ¥}½É´ôU%
ÉÑ ÅÕ½ÐíAɽɵ¹µÅÕ½ÐìµÀìÀÌØíÙÉÍ¥½¸°àÀÄ°ÐÀÄ°ÈÀÈ°ÄÈä¤

Assign('version', 'data') and $version = 'data' should be treated accordingly. You should not read the Assign() value by using $version and the vice-versa as it is illogical. If you expect that variable then declare it accordly to prevent warning. The warning is correct to me and you should adjust your declarations as suitable.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Then I use those variables in my code late down, like here:

$ui_form = GUICreate("Programname " & $version, 801, 401, 202, 129)
You can also use a function and Dim to use a variable if it has been possibly declared which may suit your needs.

$ui_form = GUICreate("Programname " & _Version(), 801, 401, 202, 129)

Func _Version()
    Dim $version
    Return $version
EndFunc

$version will equal "" if not declared globally when the function is called.

Edit:

Added possibly as optional use of global variable declaration

Edited by MHz

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