Results 1 to 10 of 10

Thread: [RESOLVED] [2008] How to check if number between 2 numbers

  1. #1

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Resolved [RESOLVED] [2008] How to check if number between 2 numbers

    hi all

    How can I check if number is between to numbers using select case
    for example to know if it's between 0-5 or 5-10 or 10-15

    please help

    thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How to check if number between 2 numbers

    vb.net Code:
    1. Select Case myNumber
    2.     Case 0 to 5
    3.  
    4.     Case 6 to 10
    5.  
    6.     Case 11 to 15
    7.  
    8.     Case Else
    9.  
    10. End Select
    or
    vb.net Code:
    1. Select Case myNumber
    2.     Case Is <= 5
    3.  
    4.     Case Is <= 10
    5.  
    6.     Case Is <= 15
    7.  
    8.     Case Else
    9.  
    10. End Select
    The first one will work for whole numbers only. The second will work for floating-point numbers too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Thumbs up Re: [2008] How to check if number between 2 numbers

    I have tried it but I typed it wrong
    like this
    Code:
    select case mynumber
    case is 0 to 5
    so it didn't work
    but the one you typed worked

    thank you for helping

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How to check if number between 2 numbers

    You only use 'Is' when the Case expression starts with an operator rather than a value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member mahammad()'s Avatar
    Join Date
    Jun 2008
    Location
    Cairo , Egypt
    Posts
    71

    Re: [2008] How to check if number between 2 numbers

    ok thanks

  6. #6
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: [2008] How to check if number between 2 numbers

    The first one will work for whole numbers only. The second will work for floating-point numbers too.
    jmcilhinney Can you please elaborate as to why it behaves like this

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How to check if number between 2 numbers

    Quote Originally Posted by aashish_9601
    jmcilhinney Can you please elaborate as to why it behaves like this
    I'm glad you asked because it made me realise that it's actually not true. They'll both work for floating point values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Addicted Member
    Join Date
    May 2008
    Location
    Denmark
    Posts
    178

    Re: [2008] How to check if number between 2 numbers

    vb.net Code:
    1. Dim number as Integer = 7
    2.  
    3. If number > 5 and number < 10 Then
    4. ' The number is between 5-10
    5. End If

    Thats i a method too i guess..

  9. #9
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2008] How to check if number between 2 numbers

    What link wrote was what i was thinking.. but then i saw that you asked for it in the select case format.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] How to check if number between 2 numbers

    Quote Originally Posted by jmcilhinney
    I'm glad you asked because it made me realise that it's actually not true. They'll both work for floating point values.
    Further to this, the range will work for floating point numbers within the range but it won't catch number between 5 and 6 or numbers between 10 and 11, so it actually isn't suitable for most scenarios where the number may not be whole.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width