Sign in to follow this  
Followers 0
gerwim

Variable used without being declared

16 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



Just copy and pasting your code into scite does not generate that error you are getting.

Even if i call your ReadIni Function.

Is there a larger piece of code this belongs to?

Share this post


Link to post
Share on other sites

Hmm, strange. My bad.

Yes, there is a bigger code, but just Functions which should not interfere with this. Nothing has anything todo with these variables.

Share this post


Link to post
Share on other sites

And the most strange thing is, the function below - WriteIni - works. Even it uses the same variable:

Func WriteIni($veld, $data)
    IniWrite($vConfigLocation, "General", $veld, $data)
EndFunc   ;==>WriteIni

Share this post


Link to post
Share on other sites

As a thought, perhaps it is because you are calling it a local variable, try setting it to Global instead and see what happens.

Share this post


Link to post
Share on other sites

No, it is set as Global (see the very first line of my code).

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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?

Edited by czardas

Share this post


Link to post
Share on other sites

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

Share this post


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.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


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.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


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!

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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()

Share this post


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()

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