未定义的变量

问题描述:

预期这将工作:未定义的变量

a := "111" 
b := "222" 

if (a != "aaa" and b != "bbb") 
    MsgBox, Yes 

但“是”的消息也将如果一个变量没有被定义

; a := "111" ; Commented line 
b := "222" 

if (a != "aaa" and b != "bbb") 
    MsgBox, Yes ; Since var "a" is not defined, I don't want this message box 

这里是所示我如何解决它:

; a := "111" 
b := "222" 

if ((a and b) and (a != "aaa" and b != "bbb")) 
    MsgBox, Yes 

但从我的角度来看,它看起来像一件可怕的事情。有没有更正确的方法?

由于and是可交换的,你可以不用括号:

if a and b and a != "aaa" and b != "bbb" 

替代解决方案

初始化变量的值,你正在测试(AAA),所以,如果你的实现代码不会改变它们,你会得到期望的结果:

a=aaa 
b=bbb 

... do some stuff ... 

global a,b 
if a != "aaa" and b != "bbb" 
    MsgBox, Yes 

说明

aundefined,好像你要undefined != "aaa"以某种方式评价来false。这就像说你想undefined == "aaa"以某种方式评估为true。你的逻辑太复杂了。

下面是你的逻辑状态表:

   Actual Desired T1  T2 
a  b  MsgBox MsgBox a!=aaa b!=bbb T1 and T2 
----- ------ ------ ------- ------ ------ ----- 
undef undef Yes  no  true true true 
undef bbb  no  no  true false false 
undef 222  Yes  no  true true true The example you didn't want 
aaa  undef no  no  false true false 
aaa  bbb  no  no  false false false 
aaa  222  no  no  false true false 
111  undef Yes  no  true true true 
111  bbb  no  no  true false false 
111  222  Yes  Yes  true true true Only one you want 

当消息框出现在您的原代码Actual MsgBox列显示。 Desired MsgBox =是的,你想要发生。 T1T2是您的条件的部分计算。 T1 and T2是您的病情的最终值。

最后一行显示您希望MsgBox出现的唯一状态;当a等于niether aaaundefinedb既不等于bbb也不等于undefined

因此,我们可以通过初始化a为“AAA”和b至“BBB”简化的逻辑。实际上,我们通过使两个值(“aaa”和undefined)等价,将每个变量的两个条件组合为单个条件。

我希望是有道理的

+0

感谢您的答案,但我的问题是关于如何避免消息,如果一个变量未定义。在你的例子中,如果我注释掉第一行('; a = aaa'),这个消息仍然会显示。这不是我想要的。 **更新:**对不起,您的实际答案是'如果a和b和a!=“aaa”和b!=“bbb”',第二个代码块只是一个加法,对不对? –

+0

第二个代码块是另一种解决方案。我添加了一个解释 –