Jump to content
elbywong

Global variable cannot be updated in function

Recommended Posts

elbywong

Hi there, I am the newbie of Autoit and deal with some issue when I am implementing the Autoit script. Can anyone help to check whether my concept or implementation is wrong or not? 

I have a <main.au3> file that contains the global variables named as "caseNo" and "caseTitle" which the variables will call in another function file named as <CommonFunc.au3>. But, the variables only able to update within the <main.au3> but not <CommonFunc.au3>.

In <main.au3>, I am trying to update the variables and the log file can show the changes of variables. For example, the string value of "CaseNo" can change from "Case01" into "Case06". However, when I am trying to capture the variables from <CommonFunc.au3> into log file, the variable of "caseNo" and "caseTitle" are both cached in "Case01" and its own title.

Can anyone help to investigate and advise on it? I am so confused and trapped into this issue for two days which I cannot figure out what went wrong....

 

 

 

main.au3

CommonFunc_modified.au3

Share this post


Link to post
Share on other sites
jchd

Do you invoke any function in CommonFunc from either
#include <login.au3> ; case 1
#include <deleteProject.au3> ; case 6
or from some other UDF you include?

If yes, then realize that at this point your global variables aren't declared yet.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
elbywong
10 minutes ago, jchd said:

Do you invoke any function in CommonFunc from either
#include <login.au3> ; case 1
#include <deleteProject.au3> ; case 6
or from some other UDF you include?

If yes, then realize that at this point your global variables aren't declared yet.

I have included all of the cases and plus with the <CommonFUnc.au3> into the <main.au3> already. I think the global variables suppose declared in <main.au3> and inherit to <CommunFunc.au3> as well :blink:

Share this post


Link to post
Share on other sites
jchd

Stop thinking forward! AutoIt uses an interpretor, it isn't a compiled language.

When AutoIt processes includes up to CommonFunc it has not yet seen declarations placed some lines after that in Main. So there is no "inheritance" possible from line 200 in line 100. When interpretor processes line 100, it can have no idea what's declared in line 200 because it can't "see" forward.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
elbywong
1 minute ago, jchd said:

Stop thinking forward! AutoIt uses an interpretor, it isn't a compiled language.

When AutoIt processes includes up to CommonFunc it has not yet seen declarations placed some lines after that in Main. So there is no "inheritance" possible from line 200 in line 100. When interpretor processes line 100, it can have no idea what's declared in line 200 because it can't "see" forward.

So, you are now suggesting me to include the case into <CommonFUnc.au3> as well? I am getting a little bit lost :'(

Share this post


Link to post
Share on other sites
jchd

Only you know what you mean by "case".

What I'm saying is that: in your #includes if you have any active statement (not within a function) referencing variables which are only defined after then you'll fall into "variable not defined" trap.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Juvigy

Define and declare ALL your global variables in the beginning of your master script.

  • Like 1

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.