if Var between LowerBound and UpperBound not working Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
theholyscripture
Posts: 62
Joined: 26 Apr 2019, 17:11

if Var between LowerBound and UpperBound not working

15 Jan 2020, 12:29

Having troubles with var between lower bound and upper bound. The statement is evaluating to true when it shouldn't be. I have an input box which prompts for an input year and then checks to see if that year falls between 2017 and the current year.

Example code in documentation:

Code: Select all

if var between %VarLow% and %VarHigh%
    MsgBox %var% is between %VarLow% and %VarHigh%.
My Code:

Code: Select all

InputBox, Request, Folder Navigator, Enter a year    ; ASSUME USER ENTERS 2015
;STOPS SCRIPT IF BLANK VALUE
If (Request = "") { 
	Return
}



;INSTRUCTIONS: ENTER THE YEAR IN THE FORMAT XXXX OR XX
Else  {
;MsgBox, % Request ; Sanity check to see that it actually stores 2015 in the Request variable
;MsgBox, % (if %Request% between 2017 and %A_YYYY%) ; Evaluates to False with values that are True (Ex 2018)
MsgBox, % (if Request between 2017 and A_YYYY) ; Evaluates to True with 2015 as input

Return
}
	
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: if Var between LowerBound and UpperBound not working  Topic is solved

15 Jan 2020, 12:59

if var between is a traditional if statement. In your code you're trying to run it as an expressional if... which will not work as expected. Instead use if (Request >=2017 && Request<=A_YYYY).
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: if Var between LowerBound and UpperBound not working

15 Jan 2020, 13:04

alternatively, use a function wrapper

Code: Select all

between(var, min, max) {
	if var between %min% and %max%
		return true
	else
		return false
}

MsgBox % between(2015, 2017, A_YYYY)
theholyscripture
Posts: 62
Joined: 26 Apr 2019, 17:11

Re: if Var between LowerBound and UpperBound not working

15 Jan 2020, 17:47

Nextron wrote:
15 Jan 2020, 12:59
if var between is a traditional if statement. In your code you're trying to run it as an expressional if... which will not work as expected. Instead use if (Request >=2017 && Request<=A_YYYY).
I see, I was hoping to use

Code: Select all

if var between lower and higher
because the code seemed cleaner but the alternative works fine.

swagfag wrote:
15 Jan 2020, 13:04
alternatively, use a function wrapper

Code: Select all

between(var, min, max) {
	if var between %min% and %max%
		return true
	else
		return false
}

MsgBox % between(2015, 2017, A_YYYY)
Good idea, I think I'll make that function for future use.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: if Var between LowerBound and UpperBound not working

15 Jan 2020, 18:13

Code: Select all

class IfVar
{
	__New(var) {
		this.var := var
	}

	between(min) {
		this.min := min

		return this
	}

	and(max) {
		var := this.var
		min := this.min

		if var between %min% and %max%
			return true
		else
			return false
	}
}

IfVar(var) {
	return new IfVar(var)
}

MsgBox % IfVar(2015).between(2017).and(A_YYYY)
yeahh dont do this
theholyscripture
Posts: 62
Joined: 26 Apr 2019, 17:11

Re: if Var between LowerBound and UpperBound not working

15 Jan 2020, 18:17

swagfag wrote:
15 Jan 2020, 18:13

Code: Select all

class IfVar
{
	__New(var) {
		this.var := var
	}

	between(min) {
		this.min := min

		return this
	}

	and(max) {
		var := this.var
		min := this.min

		if var between %min% and %max%
			return true
		else
			return false
	}
}

IfVar(var) {
	return new IfVar(var)
}

MsgBox % IfVar(2015).between(2017).and(A_YYYY)
yeahh dont do this
I'm not sure what I'm looking at so that won't be a problem lol. Haven't used Classes or this.that yet.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: edc, newbieforever and 134 guests