14 Replies - 41451 Views - Last Post: 24 February 2013 - 08:21 PM Rate Topic: -----

#1 Essel  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 118
  • Joined: 08-May 09

check if number is between two numbers inclusive

Posted 05 November 2010 - 06:25 AM

please i want a user to entera number and then check if the number entered fall between the minmum number specified and the and maximum number specified inclusive.i have this code but itis not working for me as i want.that is if the entered number is not within the min. and max. inclusive then it should promt the user.please help me.
if number>=minnuber and number <=maxinumber then
 exit sub
else
messagebod.show("number is out of range.number must be between  the minimum and the maximum number")
end if



Is This A Good Question/Topic? 0
  • +

Replies To: check if number is between two numbers inclusive

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 06:31 AM

Well, that is between 2 values inclusive
Was This Post Helpful? 0
  • +
  • -

#3 james31rock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 12-February 09

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 06:33 AM

Have you set a breakpoint and add a watch to the variables number, minnumber, and maxnumber? It seems like it should work as long as these values are set correctly. Step through the code.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 07:15 AM

After looking at your code, maybe it's more of the logic of testing the number. try this and see if it gives you the result you're wanting.
if number <= minnumber or number >= maxinumber then
messagebox.show("number is out of range.number must be between  the minimum and the maximum number")
exit sub
end if

Please copy and paste this code as I made a lot of changes that might be over-looked.

This post has been edited by CharlieMay: 05 November 2010 - 07:16 AM

Was This Post Helpful? 1
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 10:18 AM

I find it easier to interpret if you arrange the expression into the form.

LowerBound <= Value <= UpperBound

Now you can see there are two sub expressions

1. (LowerBound <= Value) <= UpperBound
2. LowerBound <= (Value <= UpperBound)

If (LowerBound <= Value) AndAlso (Value<=UpperBound) Then
 ' Is within bounds
Else
 ' Is not within bounds
End If
{/code]

If you just want to see if the value is outside the bounds, enclose both expression inside of a not.
[code]
If Not ( (LowerBound <= Value) AndAlso (Value<=UpperBound) ) Then
' Not with in bounds
Else
' Is within bounds
End If


Was This Post Helpful? 1
  • +
  • -

#6 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 01:23 PM

I don't know if it's just me, but I hate long ass comparison if cases that are just basically some math that can be abstracted.

Instead I create a Util class in which you can define the operation. Then no matter how you write the implementation, the if statement is always the same. And if you find a better way to do it, you can easily update it later on.

So then the result would be:

If MyUtil.InRange(num, min, max) Then ''do stuff



as one may notice in this class... the exact code you're expecting is like the 3rd function.

Namespace Utils

    ''' <summary>
    ''' A port of the LoDMath static member class written in AS3 under the MIT license agreement.
    ''' 
    ''' A collection of math functions that can be very useful for many things.
    ''' 
    ''' 
    ''' As per the license agrrement of the lodGameBox license agreement
    ''' 
    ''' Copyright (c) 2009 Dylan Engelman
    '''
    '''Permission is hereby granted, free of charge, to any person obtaining a copy
    '''of this software and associated documentation files (the "Software"), to deal
    '''in the Software without restriction, including without limitation the rights
    '''to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    '''copies of the Software, and to permit persons to whom the Software is
    '''furnished to do so, subject to the following conditions:
    '''
    '''The above copyright notice and this permission notice shall be included in
    '''all copies or substantial portions of the Software.
    '''
    '''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    '''IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    '''FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    '''AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    '''LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    '''OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    '''THE SOFTWARE.
    ''' 
    ''' http://code.google.com/p/lodgamebox/source/browse/trunk/com/lordofduct/util/LoDMath.as
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    Public NotInheritable Class MathUtil

#Region "Public ReadOnly Properties"
        Public Const PI As Double = 3.1415926535897931 ' Number pi
        Public Const PI_2 As Double = 1.5707963267948966 ' PI / 2 OR 90 deg
        Public Const PI_4 As Double = 0.78539816339744828 ' PI / 4 OR 45 deg
        Public Const PI_8 As Double = 0.39269908169872414 ' PI / 8 OR 22.5 deg
        Public Const PI_16 As Double = 0.19634954084936207 ' PI / 16 OR 11.25 deg
        Public Const TWO_PI As Double = 6.2831853071795862 ' 2 * PI OR 180 deg
        Public Const THREE_PI_2 As Double = 4.71238898038469 ' 3 * PI_2 OR 270 deg
        Public Const E As Double = 2.71828182845905 ' Number e
        Public Const LN10 As Double = 2.3025850929940459 ' ln(10)
        Public Const LN2 As Double = 0.69314718055994529 ' ln(2)
        Public Const LOG10E As Double = 0.43429448190325182 ' logB10(e)
        Public Const LOG2E As Double = 1.4426950408889634 ' logB2(e)
        Public Const SQRT1_2 As Double = 0.70710678118654757 ' sqrt( 1 / 2 )
        Public Const SQRT2 As Double = 1.4142135623730951 ' sqrt( 2 )
        Public Const DEG_TO_RAD As Double = 0.017453292519943295 ' PI / 180
        Public Const RAD_TO_DEG As Double = 57.295779513082323 '  180.0 / PI

        Public Const B_16 As Integer = 65536 ' 2^16
        Public Const B_31 As Long = 2147483648 ' 2^31
        Public Const B_32 As Long = 4294967296 ' 2^32
        Public Const B_48 As Long = 281474976710656 ' 2^48
        Public Const B_53 As Long = 9007199254740992 ' 2^53 !!NOTE!! largest accurate double floating point whole value
        Public Shared ReadOnly B_63 As ULong = CULng("9223372036854775808") ' 2^63
        Public Const B_64_m1 As ULong = ULong.MaxValue '18446744073709551615 or 2^64 - 1 or ULong.MaxValue...

        Public Const ONE_THIRD As Double = 0.33333333333333331 '  1.0/3.0
        Public Const TWO_THIRDS As Double = 0.66666666666666663 '  2.0/3.0
        Public Const ONE_SIXTH As Double = 0.16666666666666666 '  1.0/6.0

        Public Const COS_PI_3 As Double = 0.8660254037844386 ' COS( PI / 3 )
        Public Const SIN_2PI_3 As Double = 0.03654595 '  SIN( 2*PI/3 )

        Public Const CIRCLE_ALPHA As Double = 0.55228474983079345 ' 4*(Math.sqrt(2)-1)/3.0

        Public Const ONN As Boolean = True
        Public Const OFF As Boolean = False

        Public Const SHORT_EPSILON As Double = 0.1 ' round integer epsilon
        Public Const PERC_EPSILON As Double = 0.001 ' percentage epsilon
        Public Const EPSILON As Double = 0.0001 ' single float average epsilon
        Public Const LONG_EPSILON As Double = 0.00000001 ' arbitrary 8 digit epsilon
        Public Shared ReadOnly MACHINE_EPSILON As Double = MathUtil.ComputeMachineEpsilon()


        Public Shared Function ComputeMachineEpsilon() As Double
            Dim fourThirds As Double = 4.0 / 3.0
            Dim third As Double = fourThirds - 1.0
            Dim one As Double = third + third + third
            Return Math.Abs(1.0 - one)
        End Function
#End Region

#Region "Public Shared Methods"

        Public Shared Function Largest(ByVal ParamArray args() As Double) As Double
            If args.Length < 1 Then Return Double.NaN

            Dim res As Double = args(0)

            For i As Integer = 1 To UBound(args)
                If args(i) > res Then res = args(i)
            Next

            Return res
        End Function

        Public Shared Function Smallest(ByVal ParamArray args() As Double) As Double
            If args.Length < 1 Then Return Double.NaN

            Dim res As Double = args(0)

            For i As Integer = 1 To UBound(args)
                If args(i) < res Then res = args(i)
            Next

            Return res
        End Function

        Public Shared Function InRange(ByVal value As Double, ByVal max As Double, Optional ByVal min As Double = 0) As Boolean
            Return (value >= min AndAlso value <= max)
        End Function

REDACTED - several lines redacted seeing as it doesn't like to put it into a scrolly box...

    End Class
End Namespace


This post has been edited by lordofduct: 05 November 2010 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 02:44 PM

Or more generically in this snippet

Edit: lordofduct you've made an fundamental error in code you've shown. In .net arrays are Zero-Index based.

This post has been edited by AdamSpeight2008: 05 November 2010 - 02:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 638
  • Joined: 03-February 10

Re: check if number is between two numbers inclusive

Posted 05 November 2010 - 11:14 PM

So if you are allowing a number to be entered and the assumption is this a windows application, then why not use a NumericUpDown control as the input. With this control you set the max and min values either in the designer or at runtime. Then the user cannot enter a value outside of the range you want to be limited to. I do still agree that you may want to do the validation of the input, however if you can limit it with the first user interaction, then as programmers we should.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: check if number is between two numbers inclusive

Posted 06 November 2010 - 11:42 AM

The O.P. doesn't mention that the number is coming from a UI element.
Was This Post Helpful? 0
  • +
  • -

#10 nkasei28  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 93
  • Joined: 10-May 09

Re: check if number is between two numbers inclusive

Posted 06 November 2010 - 03:41 PM

Try this one. I've tested it and it works perfectly.

'if the number does not fall within the minimum and maximum numbers inclusive, the function should return, else print the MsgBox.
if Not (number>=minnuber) or Not(number <=maxinumber) then 

 exit sub  

 else 

 messagebox.show("number is out of range.number must be between  the minimum and the maximum number")  

 end if 



Was This Post Helpful? 1
  • +
  • -

#11 hutchy4122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-October 11

Re: check if number is between two numbers inclusive

Posted 11 October 2012 - 09:47 AM

Here's a slightly different method using select case statements.


Select Case number
    Case minnumber to maxnumber : Exit Select
    Case Else : MsgBox("The number is outside of the range. It must be between " & minnumber & " and " & maxnumber & ".")
End Select



Was This Post Helpful? 0
  • +
  • -

#12 nargsbrood  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Re: check if number is between two numbers inclusive

Posted 24 February 2013 - 04:21 PM

I know this is old but no one explained with the OP code does not work. The reason why does not work is the compiler was interpreting it differently than Essel was interpreting it.

Here is how the compiler was interpreting it:
if (((number >= minnumber) and number) <= maxnumber) then
     messagebox.show("number is out of range.number must be between  the minimum and the maximum number")
     exit sub
end if



Result:
(number >= minnumber)                                =    True
((number >= minnumber) And number)                   =    5
(((number >= minnumber) And number) <= maxnumber)    =    5



Essentially it reads it as:
If 5 then



When doing conditional operations, it will implicitly convert integers to booleans and interpret everything that is non-zero as true. If you turn Option Strict on then it will not let you run the application with the OP code because it no longer allows implicit conversions.

The whole problem could have been fixed using:
if (number >= minnumber) and (number <= maxnumber) then
     messagebox.show("number is out of range.number must be between  the minimum and the maximum number")
     exit sub
end if


Was This Post Helpful? -1
  • +
  • -

#13 nargsbrood  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Re: check if number is between two numbers inclusive

Posted 24 February 2013 - 04:29 PM

I created this account to post the above explanation. Bummer - Due to not having 15 posts, I cannot edit my post to fix grammatical errors. <_</>
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,346
  • Joined: 12-December 12

Re: check if number is between two numbers inclusive

Posted 24 February 2013 - 05:22 PM

The previous post is not correct. Relational operators ( < > etc.) have a higher precedence than logical (And) operators.
Was This Post Helpful? 0
  • +
  • -

#15 nargsbrood  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 24-February 13

Re: check if number is between two numbers inclusive

Posted 24 February 2013 - 08:21 PM

View Postandrewsw, on 24 February 2013 - 05:22 PM, said:

The previous post is not correct. Relational operators ( < > etc.) have a higher precedence than logical (And) operators.


:whistling: absolutely right... thanks!

I would like to know what he thought the problem was in his code for why it wasn't working...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1