Jump to content

strange error


keelaji
 Share

Recommended Posts

HotKeySet ("{F10}", "Terminate")

Func Terminate()
    MsgBox( 0, "stop", "stopped")
    Exit
EndFunc   ;==>Terminate

 HotKeySet ("{1}", uno())
 HotKeySet ("{2}", due())

$a = 0

 Func uno()
     $a = WinGetHandle("")
 EndFunc

Func due()
    WinActivate($a)
EndFunc


While 1
WEnd

this code return this error whenever I call a function: 

"D:\autoit scripts\PROVA\prova.au3" (30) : ==> Variable used without being declared.:
WinActivate($a)
WinActivate(^ ERROR

what am I doing wrong ?

Link to comment
Share on other sites

  • Moderators

You're declaring it after you set a function to call another function that calls it.

And to be honest, you're not  "really" declaring it.

This will work:

HotKeySet ("{F10}", "Terminate")

Func Terminate()
    MsgBox( 0, "stop", "stopped")
    Exit
EndFunc   ;==>Terminate


$a = 0

 HotKeySet ("{1}", uno())
 HotKeySet ("{2}", due())

 Func uno()
     $a = WinGetHandle("")
 EndFunc

Func due()
    WinActivate($a)
EndFunc


While 1
WEnd

 

But I'd expect to see something more like:

Global $a = 0
HotKeySet ("{F10}", "Terminate")
HotKeySet ("{1}", uno())
HotKeySet ("{2}", due())

Func Terminate()
    MsgBox( 0, "stop", "stopped")
    Exit
EndFunc   ;==>Terminate

 Func uno()
     $a = WinGetHandle("")
 EndFunc

Func due()
    WinActivate($a)
EndFunc

While 1
    Sleep(10)
WEnd

 

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

5 minutes ago, SmOke_N said:

You're declaring it after you set a function to call another function that calls it.

And to be honest, you're not  "really" declaring it.

This will work:

HotKeySet ("{F10}", "Terminate")

Func Terminate()
    MsgBox( 0, "stop", "stopped")
    Exit
EndFunc   ;==>Terminate


$a = 0

 HotKeySet ("{1}", uno())
 HotKeySet ("{2}", due())

 Func uno()
     $a = WinGetHandle("")
 EndFunc

Func due()
    WinActivate($a)
EndFunc


While 1
WEnd

 

But I'd expect to see something more like:

Global $a = 0
HotKeySet ("{F10}", "Terminate")
HotKeySet ("{1}", uno())
HotKeySet ("{2}", due())

Func Terminate()
    MsgBox( 0, "stop", "stopped")
    Exit
EndFunc   ;==>Terminate

 Func uno()
     $a = WinGetHandle("")
 EndFunc

Func due()
    WinActivate($a)
EndFunc

While 1
    Sleep(10)
WEnd

 

Thank you smoke, it obviously works now.

I didn't quite understood the importance of Local and Global, because it seems working even if I don't use those.

Link to comment
Share on other sites

  • Moderators

That's true, it will still work, any variable outside of a function is declared global and any inside a local.  But, it's bad practice in many eyes to just make yourself or others assume, plus other languages aren't so lenient.

It wasn't the declaration that fixed the issue, it was putting the variable above the function that called the function that used the variable.

eg.

Your hotkey calls "due()", due() uses the global variable $a, because the code works from the top down, due() was checked before the global variable $a was declared

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

14 minutes ago, SmOke_N said:

That's true, it will still work, any variable outside of a function is declared global and any inside a local.  But, it's bad practice in many eyes to just make yourself or others assume, plus other languages aren't so lenient.

It wasn't the declaration that fixed the issue, it was putting the variable above the function that called the function that used the variable.

eg.

Your hotkey calls "due()", due() uses the global variable $a, because the code works from the top down, due() was checked before the global variable $a was declared

ok, thank you for your time. :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...