Number in a range

Post

Posted
Rating:
#1 (In Topic #1257)
Regular
rj71 is in the usergroup ‘Regular’
 Is there an simple/direct way to see if a number in a valuebox is within a range? Like is valuebox1.value number between 40 and 60 or whatever the routine calls for.
I'm not seeing anything on the wiki page under arithmetic operators or in the forum. It's ok if I have to McGuyver it but I thought there might be a quicker and more simple way to do this.
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
You could use the AND operator:

Code (gambas)

  1. If (ValueBox1.Value > 39) AND (ValueBox1.Value < 61) Then ....

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
rj71 is in the usergroup ‘Regular’

vuott said

You could use the AND operator:

Code (gambas)

  1. If (ValueBox1.Value > 39) AND (ValueBox1.Value < 61) Then ....

AH!! that's what I was trying I just didn't get the syntax right  :lol:  thank you!
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
If the testing values are subject to change then make it a Function.

Code (gambas)

  1. Public Function IsBetween(InTestValue as Integer, LowValue as Integer, HighValue as Integer) as Boolean
  2. Dim RetBool as Boolean = False
  3.  
  4. If (InTestValue > LowValue) AND (InTestValue < HighValue) then RetBool = True
  5.  
  6. Return RetBool
  7.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
BEWARE!
ValueBox.Value is a Variant. So Vuott's answer will be true if ValueBox.Value is 39.1 or 60.9
Use

Code (gambas)

  1. If (ValueBox1.Value >= 40) AND (ValueBox1.Value <= 60) Then ...
ALSO
Q's answer can be shortened

Code (gambas)

  1. Public Function IsBetween(InTestValue as Integer, LowValue as Integer, HighValue as Integer) as Boolean
  2.  
  3.   Return  (InTestValue > LowValue) AND (InTestValue < HighValue)
  4.  

Note that by using Q's function the variants (floats) will be automatically interpreted as integers. Therefore the valid range is 41 to 59. IOW it is an exclusive range.
b

Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

thatbruce said

ValueBox.Value is a Variant. So Vuott's answer will be true if ValueBox.Value is 39.1 or 60.9
..but rj71 wrote two integer values in his example.
I simply stuck to his example.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
Also select

Code (gambas)

  1.  
  2. Case 30 To 45
  3. ' do something
  4.  
  5. Case 46 To 60
  6. ' do something else
  7.  
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
rj71 is in the usergroup ‘Regular’
 Thanks for everyone's input. What vuott gave me was perfectly fine. I pretty much had that I just screwed up the syntax a bit. What i'm doing is not anything that needs to be precise so it's all good. Thanks again everyone!
Online now: No Back to the top
1 guest and 0 members have just viewed this.