Page 1 of 1

Elegant way to check variables before using them

Posted: 05 May 2017, 13:44
by john_c
I want to have a message box if var

Code: Select all

a != "aaa"
and var

Code: Select all

b != "bbb"
That is how I do it now:

Code: Select all

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

if (a != "aaa" and b != "bbb")
    MsgBox, Yes
The problem is, that in my real-life code var a is sometimes undefined. So, I don't want to see message box in this case, but it still appears:

Code: Select all

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

if (a != "aaa" and b != "bbb")
    MsgBox, Yes ; Now I don't want this message box to be appeared, but it is till shown, despite the fact that var a is now undefined
That is my current way to fix this issue:

Code: Select all

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

if ((a and b) and (a != "aaa" and b != "bbb"))
    MsgBox, Yes
But I don't like this way, because it seems too ugly. Is there more elegant way for it? i.e. I'm searching something like this:

Code: Select all

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

IfAllVarsDefined
{
    if ((a and b) and (a != "aaa" and b != "bbb"))
        MsgBox, Yes ; This message box will not be shown now. Ant that is good.
}

Re: Elegant way to check variables before using them

Posted: 05 May 2017, 14:45
by BoBo

Code: Select all

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

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

Code: Select all

a := "aaa"
b := "222"

result := if (a != "aaa") && (b != "bbb") ? true : false
    MsgBox % result

Re: Elegant way to check variables before using them

Posted: 05 May 2017, 15:33
by john_c
BoBo wrote:

Code: Select all

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

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

Code: Select all

a := "aaa"
b := "222"

result := if (a != "aaa") && (b != "bbb") ? true : false
    MsgBox % result
Hm, thanks for code, but it doesn't do the task. In both your examples, if one of vars is undefined, the message box is still shown. It should not be shown. (The message box should be shown only if both vars are defined).

Re: Elegant way to check variables before using them

Posted: 05 May 2017, 21:52
by Soft

Code: Select all

;a := 111
b := 222

If (StrLen(a) && StrLen(b))
{
	Msgbox, Won't be showed
}