Jump to content

Variable used without being declared


Recommended Posts

Hi there,

For some reason AutoIt says "Variable used without being declared" when it definately is declared:

Global $vConfigLocation

$vConfigLocation = IniRead(@AppDataDir & "\locations.ini", "Locations", @AutoItExe, "notfound")
if ($vConfigLocation = "notfound") Then
    MsgBox(16, "Error", "Could not read the locations file. Make sure your %appdata% folder is writeable.")
    Exit
Else
    $vConfigLocation = @AppDataDir & "\" & $vConfigLocation & ".ini"
EndIf

Func ReadIni($veld)
    Local $vReturn = IniRead($vConfigLocation, "General", $veld, "notfound")
    if ($vReturn = "notfound") Then
        Return False
    Else
        Return $vReturn
    EndIf
EndFunc   ;==>ReadIni

The error I get:

Local $vReturn = IniRead($vConfigLocation, "General", $veld, "notfound")
Local $vReturn = IniRead(^ ERROR

Anyone knows what the problem is?

Edited by gerwim
Link to post
Share on other sites

It could be the variable $void that hasn't been declared.

Edit Something that puzzles me: I don't understand the reason for the function ReadIni - why write another IniRead function?

There's no variable "$void", but "$veld". "$veld" is passed through the function.

Since I'm using a (global) function to have one path (I prefer that over variables).

Well, i am at a loss as to what is causing your problem since i am unable to reproduce your error.

Yes, I'm afraid I can't post the complete code, but I'll try to strip things off to see what causes it.

Link to post
Share on other sites

There's no variable "$void", but "$veld". "$veld" is passed through the function.

Since I'm using a (global) function to have one path (I prefer that over variables).

Yes, I'm afraid I can't post the complete code, but I'll try to strip things off to see what causes it.

Sorry the screen I use makes my eyes tired sometimes. Guess I need glasses. $veld could be the variable that hasn't been declared. I don't see where you declare it. Edited by czardas
Link to post
Share on other sites

Sorry the screen I use makes my eyes tired sometimes. Guess I need glasses. However the string "$veld" is written as a variable because it isn't wrapped in quotes. This will be interpreted as a variable that hasn't been declared, unless you write it as a string.

Yes I know, but it's from the function itself. This is the correct way, and it works. The problem is the variable $vConfigLocation.

Link to post
Share on other sites

Yes I know, but it's from the function itself. This is the correct way, and it works. The problem is the variable $vConfigLocation.

I don't know, that seems okay to me. $vConfigLocation has been declared, so it has to be something else. The part we can't see is where $veld is declared. I mean you're not calling on the function in the code, so it's impossible to tell what's wrong. Edited by czardas
Link to post
Share on other sites

Alright, got it. Silly error of mine. I define a complete Global variable list at the top of my script. So I had:

Global $vAutoUpdate = ReadIni("autoupdate")
Global $vAutoReconnect = ReadIni("autoreconnect")
Global $vConfigLocation

But that will call the function ReadIni which doesn't know $vConfigLocation (yet).

Thanks all!

Link to post
Share on other sites

Reorder the declarations this way to fix it.

Global $vConfigLocation
Global $vAutoUpdate = ReadIni("autoupdate")
Global $vAutoReconnect = ReadIni("autoreconnect")

Although this apparently fixes the error message, I'm not sure exactly how your code works, but you should declare your variables just before you use them. This may not have fixed all the problems you encounter with this code. You may need to order things differently. For example:

Global $vConfigLocation

$vConfigLocation = IniRead(@AppDataDir & "\locations.ini", "Locations", @AutoItExe, "notfound")
if ($vConfigLocation = "notfound") Then
    MsgBox(16, "Error", "Could not read the locations file. Make sure your %appdata% folder is writeable.")
    ;Exit
Else
    $vConfigLocation = @AppDataDir & "\" & $vConfigLocation & ".ini"
EndIf

Global $vAutoUpdate = ReadIni("autoupdate")
Global $vAutoReconnect = ReadIni("autoreconnect")

Func ReadIni($veld)
    Local $vReturn = IniRead($vConfigLocation, "General", $veld, "notfound")
    if ($vReturn = "notfound") Then
        Return False
    Else
        Return $vReturn
    EndIf
EndFunc   ;==>ReadIni

Edit

=> I would also change the Global declarations to Local. I hope this fixes your problem. :unsure: Edited by czardas
Link to post
Share on other sites
  • 1 year later...

english

Portugues

Translated by google

(sorry for any mistakes)

Traduzido pelo google

(desculpe pelos eventuais erros)

I had the same problem.

I realized that the order of declaration is very important to see the example idiot:

eu tive o mesmo problema.

percebi que a ordem de declaração é muito importante veja o exemplo idiota:

JanelaAtiva()

;Basta indicar o PID para usar a rotina

Global Const $hPSAPI = DllOpen("psapi.dll")

Global Const $hKERNEL32 = DllOpen("kernel32.dll")

Error

$aProcessHandle = DllCall($hKERNEL32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)

$aProcessHandle = DllCall(^ ERROR

Solution:

Solução:

Global Const $hPSAPI = DllOpen("psapi.dll")

Global Const $hKERNEL32 = DllOpen("kernel32.dll")

JanelaAtiva()

Link to post
Share on other sites

english Portugues

Translated by google Traduzido pelo google

(sorry for any mistakes) (desculpe pelos eventuais erros)

I had the same problem. eu tive o mesmo problema.

I realized that the order of declaration is very percebi que a ordem de declaração é muito

important to see the example idiot: importante veja o exemplo idiota:

JanelaAtiva()

Global Const $hPSAPI = DllOpen("psapi.dll")

Global Const $hKERNEL32 = DllOpen("kernel32.dll")

Error

$aProcessHandle = DllCall($hKERNEL32, "int", "OpenProcess", "int", 0x0400 + 0x0010, "int", 0, "int", $i_PID)

$aProcessHandle = DllCall(^ ERROR

Solution: Solução:

Global Const $hPSAPI = DllOpen("psapi.dll")

Global Const $hKERNEL32 = DllOpen("kernel32.dll")

JanelaAtiva()

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.

×
×
  • Create New...