如何在没有“可能在声明前使用"的情况下访问局部范围内的全局变量?-错误? [英] How do I access global variables in local scope without "possibly used before declaration" -errors?

查看:19
本文介绍了如何在没有“可能在声明前使用"的情况下访问局部范围内的全局变量?-错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此警告:警告:$a 可能在声明前使用. 对于我的函数:

I'm getting this warning: WARNING: $a possibly used before declaration. for my function:

Func test()
    If (IsDeclared("a")) Then
        ConsoleWrite($a)
    Else
        ConsoleWrite("Not declared")
    EndIf
EndFunc

Global $a = "test"

test()

可以通过使用全局变量作为函数的参数来避免.但我需要这个构造,因为它与我不想每次需要变量时都执行的文件操作相关联.

Can be avoided by using the global variable as a parameter to the function. But I need this construct because it's tied to a file-operation which I don't want to execute every time I need the variable.

如何在不生成可能在声明前使用"-错误​​的情况下执行此操作?

How can I do this without generating a "possibly used before declaration" -error?

推荐答案

首先,这个警告是由 Au3Check.exe 报告的,Au3Check.exe 是一个独立于 AutoIt 编译器本身的程序.您会发现在打印此警告后,您的程序仍会编译/运行.一种可能的解决方案是忽略它,或者不在您的代码上运行 Au3Check(不推荐).

Firstly, this warning is reported by Au3Check.exe, a program that is separate from the AutoIt compiler itself. You'll find that your program will still compile/run after this warning is printed. One possible solution is to just ignore it, or not run Au3Check on your code (not recommended).

如果您只是在最后定义了函数,您的代码就可以正常工作.您可能已经意识到这一点,而且我知道当您使用 #include 函数时,它们可能会位于顶部.

Your code will work fine if you simply defined your functions at the end. You were probably already aware of this, and I know when you #include functions they will likely be at the top.

如果您真的很烦人,另一种可能的解决方案是使用 Eval.我不建议这样做,因为它不需要并且如果使用 Au3Stripper(曾经是混淆器)会破坏你的脚本,尽管你的代码已经被 IsDeclared 破坏了.

Another possible solution if it really is annoying you is to use Eval. I don't recommend this as it's not needed and breaks your script if using Au3Stripper (used to be obfuscator) though your code is already broken by the use of IsDeclared.

您可能没有想到的解决方案(可能是最好的解决方案)是为此使用 Dim.

A solution (probably the best solution) you probably wouldn't have thought of is using Dim for this.

Func test()
    If(IsDeclared("a")) Then
        Dim $a

        ConsoleWrite($a & @LF)
    Else
        ConsoleWrite("Not declared" & @LF)
    EndIf
EndFunc

Global $a = "test"

test()

基本上,Dim 的作用类似于 Local,除非具有该名称的变量已存在于全局作用域中,在这种情况下,它会被重用.

Basically, Dim acts like Local, unless a variable with that name already exists in the global scope, in which case it is reused.

这篇关于如何在没有“可能在声明前使用"的情况下访问局部范围内的全局变量?-错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆