Jump to content
adjist

Variable used without being declared even though it is defined

Recommended Posts

Hello all! 

 

Getting this error :

(22) : ==> Variable used without being declared.:
if $vNumber = 0 Then
if ^ ERROR

 

But I'm sure I have defined the variable, as in the top of my script has 

Global $vNumber = 0

 

How would I go about fixing this?

 

Share this post


Link to post
Share on other sites

Oh, I think I have figured it out. I no longer get an error any more when I put 

Global $vNumber

In the beginning of each of my functions

Edited by adjist

Share this post


Link to post
Share on other sites

That sounds very wrong if it’s global it’s scope is global to the entire program and you shouldn’t have to declare it more than once


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
22 minutes ago, Earthshine said:

That sounds very wrong if it’s global it’s scope is global to the entire program and you shouldn’t have to declare it more than once

erm.. forget I said that

 

here is what I said on another post, this is pretty much what I am wanting to do

19 minutes ago, adjist said:

I see,

So you're saying , set something such as ?

Global $example = 0

At the top of my script, and inside the func I put ?

Func example()
$example = 1

;code

$example = 0
EndFunc

But when I do this, I get this error,

Variable used without being declared.

 

 

Share this post


Link to post
Share on other sites

You should post your entire code because the example above wouldn't give you an error.

Global $example = 0
example()
MsgBox(4096, "", "$example = " & $example)

Func example()
    $example = 1
    MsgBox(4096, "", "$example = " & $example)
    $example = 0
EndFunc

 

Share this post


Link to post
Share on other sites

wow.

 

i had accidentally called my functions before declaring the global value

everything works now

I'm an idiot!

Share this post


Link to post
Share on other sites

That’s why I ask you to post your whole script so we could see what’s going on


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites

Didn't you try to run such a snippet?


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
1 minute ago, jchd said:

Didn't you try to run such a snippet?

Yes.

I guess functions cannot update variables globally?

Share this post


Link to post
Share on other sites

Yes you can!

RTFM and see that's precisely the purpose of Global.


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
1 minute ago, jchd said:

Yes you can!

RTFM and see that's precisely the purpose of Global.

Sorry, I meant that I guess that functions cannot update variables while they are inside a loop

Share this post


Link to post
Share on other sites

Wrong guess again.
What makes you believe that?


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
Just now, jchd said:

Wrong guess again.
What makes you believe that?

Well, if I try to change a variable in a loop such as

Func yea()
    While(1)
        $test = 1
        ;code
        $test = 0
    WEnd
EndFunc

and have an if statement (inside another function) such as 

func example()
    if $test = 1 Then
        ConsoleWrite("hello")
    EndIf
EndFunc

It won't print the message

If there is a way to do it, could you be able to tell me how?

Share this post


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

It won't print the message

No need to try to run the code, it's obvious the message can't be printed.

Beside the fact that the present code will never exit the function yea() since it contains an infinite loop without exit route, you reset $test to 0 before invoking example(), so no wonder the msg never prints.


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

Runable code:

Global $test = 0

yea()
example()

Func yea()
    Local $t = TimerInit()
    Do
        $test += 1
    Until TimerDiff($t) > 1000
EndFunc

func example()
    if $test > 0 Then
        ConsoleWrite("$test is now " & $test & @LF)
    EndIf
EndFunc

 


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

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kingjacob90
      Hi
      So I am trying to click the green button, this button is not always in the same place. So fare I am trying to click it by finding the color but there is also something else with the same color on the screen (circled in yellow) that is causing issues. Is there a way to use the Title and Class of the window (can't be just the window as there are more than one with the same name).
      How does AutoIt Info get this information?

    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
       
      get Json.au3 from here Json.au3
       
       
      JsonVar 2019.01.25.1.zip
    • By MarkIT
      Hi AutoIT masters,
      Good day! Sorry to have bothered this forum but we really need help. We are working on an automation project that is running on VDI server. The BOTS are in .exe are running fine until AV detected them and deleted the files. The files were re-compiled and AV kept on deleting them. The copy of the .exe BOT deleted were sent to Symantec for whitelisting. After whitelisting, it is no longer deleted but no longer working as designed (showing Line script error). We checked the scripts and there were no issues since we run it using SciTE editor and it performed the desired task. Good thing we found on this thread the solution using .a3x and the BOTS worked fine and no longer deleted. Now, the problem is they are asking why the BOTS won't run in .EXE and what is the reason behind Symantec AV deleting them. We raised a case with Symantec but they cannot provide further information as they are always seeing the file as "False Positive". We even tested with Symantec turned off and those .EXE files are working fine, however, after re-enabling, it got deleted.
      Just seeking help on how to better convince them that it is really Symantec causing the issue and the .a3x file.
    • By AutoitMike
      Scite 3.4.4
      Win 10
      I click "Help" or press F1, there is no response
      If I use the file explorer and double click Autoit.chm or Autoit3.chm help opens.
      There is no dialog to check or uncheck "Always ask before opening this file" when clicking on these files.
       
      If you are curious as to why I dont have the latest version, I am creating a back up laptop that has a VERY extensive automation application that I have written over the past 15 years.
       
      An extremely potent, powerful, needed function has been deleted in the upgrade of Autoit in recent years that I can not do without. If my main laptop dies, which it almost did, I am in a very bad position. So I bought the exact same laptop and I am "cofiguring" it to work exactly the same as my main laptop. However, this one has been "Upgraded" to Win 10 which I hope is not the problem. 
      Thanks for any help
    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
×
×
  • Create New...