Elegant way to check variables before using them

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Elegant way to check variables before using them

05 May 2017, 13:44

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.
}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Elegant way to check variables before using them

05 May 2017, 14:45

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
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Elegant way to check variables before using them

05 May 2017, 15:33

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).
User avatar
Soft
Posts: 174
Joined: 07 Jan 2015, 13:18
Location: Seoul
Contact:

Re: Elegant way to check variables before using them

05 May 2017, 21:52

Code: Select all

;a := 111
b := 222

If (StrLen(a) && StrLen(b))
{
	Msgbox, Won't be showed
}
AutoHotkey & AutoHotkey_H v1.1.22.07

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 424 guests