Results 1 to 11 of 11

Thread: In between numbers. Help.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    In between numbers. Help.

    Code:
        Select Case CInt(TextBox1.Text)
                Case Is < 20
                    MsgBox("test1")
                Case Is >= 20 <= 40
                    MsgBox("test2")
                Case Is >= 40 <= 60
                    MsgBox("test3")
    
            End Select
    So i have this... but it doesnt seem to work at test3.

    Any ideas? I've tried if statements as well...

    I am trying to get an output every time a number is in between so. greater than 20 but less than 40.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: In between numbers. Help.

    Well, first of all, let's say the number is 40, if your code above would have worked the second case statement would be evaluated as true and it wouldn't reach the third even if that is also true. What I mean is that only one case statement will be executed and it will be the first that match the criteria. If you want to test for more than one value you can separate them with comma.
    Code:
    Case Is >= 20, Is <= 40
    However the above means if the number is greater or equal to 20 OR less than or equal to 40, which would always be true since one of them would always be true regardless if the number would be -37 (less than 40) or 194 (greater than 20).

    You don't need to combine them like that because if the number would be less than 20 the first case statement would be true, so if the second case statement is evaluated you already know that the first isn't true so the number must be 20 or greater.
    Code:
    Select Case number
      Case Is < 20
        'do something
      Case Is <= 40
        'number is 20 or above but not higher than 40
      Case Is <= 60
        'must be higher than 40
      Case Else
        'In all other cases, so it must be higher than 60
    End Select
    Last edited by Joacim Andersson; May 22nd, 2013 at 06:06 AM.

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

    Re: In between numbers. Help.

    You only need to specify one number in each case. In the first case you specify less than or equal to 20. In the second case you specify less than or equal to 40. If you make it to the second case then you already know the value greater than 20 because it would otherwise have been caught by the first case, so greater than 20 is implicit. The same goes for each subsequent case, e.g.
    Code:
    Dim number As Integer
    
    If Integer.TryParse(TextBox1.Text, number) Then
        Select Case number
            Case Is <= 20
                MessageBox.Show("Input is in range 0 <= N <= 20.")
            Case Is <= 40
                MessageBox.Show("Input is in range 20 < N <= 40.")
            Case Is <= 60
                MessageBox.Show("Input is in range 40 < N <= 60.")
            Case Else
                MessageBox.Show("Input is greater than 60.")
        End Select
    Else
        MessageBox.Show("Input is not a valid integer.")
    End If
    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: In between numbers. Help.

    Quote Originally Posted by jmcilhinney View Post
    You only need to specify one number in each case. In the first case you specify less than or equal to 20. In the second case you specify less than or equal to 40. If you make it to the second case then you already know the value greater than 20 because it would otherwise have been caught by the first case, so greater than 20 is implicit. The same goes for each subsequent case, e.g.
    Code:
    Dim number As Integer
    
    If Integer.TryParse(TextBox1.Text, number) Then
        Select Case number
            Case Is <= 20
                MessageBox.Show("Input is in range 0 <= N <= 20.")
            Case Is <= 40
                MessageBox.Show("Input is in range 20 < N <= 40.")
            Case Is <= 60
                MessageBox.Show("Input is in range 40 < N <= 60.")
            Case Else
                MessageBox.Show("Input is greater than 60.")
        End Select
    Else
        MessageBox.Show("Input is not a valid integer.")
    End If
    would'nt that show 3 textboxes if the number is between 1 and 20? because all of them are under 60?
    Sorry if you implied that in your sentence.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: In between numbers. Help.

    Quote Originally Posted by tommyhoogstra View Post
    would'nt that show 3 textboxes if the number is between 1 and 20? because all of them are under 60?
    Sorry if you implied that in your sentence.
    No. Take a look at the documentation to see how Select Case works. http://msdn.microsoft.com/en-us/library/cy37t14y.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: In between numbers. Help.

    Quote Originally Posted by dbasnett View Post
    No. Take a look at the documentation to see how Select Case works. http://msdn.microsoft.com/en-us/library/cy37t14y.aspx
    Thanks for the reply.
    I have another problem, because I am using multiple factors I had to move back to if statements, how would I go about this
    Code:
     If CInt(TextBox7.Text) <= 20 And cbxFace.Checked = True And CbCorpChild.Text = "Corporate" Then
                Class1.facecorp = 55
            ElseIf CInt(TextBox7.Text) >= 40 < 60 And cbxFace.Checked = True And CbCorpChild.Text = "Corporate" Then
                Class1.facecorp = 110
    End if
    Im not sure if that would use with a select case So i just went the so called easy way out.

    Thanks.

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

    Re: In between numbers. Help.

    We'd have to know all the requirements to know for sure but, given that it appears that only one of your conditions is changing, you could just put a Select Case inside an If:
    Code:
    If cbxFace.Checked AndAlso CbCorpChild.Text = "Corporate" Then
        Select Case CInt(TextBox7.Text)
    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

    Thread Starter
    Junior Member
    Join Date
    May 2013
    Posts
    31

    Re: In between numbers. Help.

    Quote Originally Posted by jmcilhinney View Post
    We'd have to know all the requirements to know for sure but, given that it appears that only one of your conditions is changing, you could just put a Select Case inside an If:
    Code:
    If cbxFace.Checked AndAlso CbCorpChild.Text = "Corporate" Then
        Select Case CInt(TextBox7.Text)
    Thanks

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: In between numbers. Help.

    Note that AndAlso should always be used in place of And unless you are doing bit manipulation. AndAlso is more efficient. OrElse should be used in place of Or, too. Take a look at the documentation to see why, as there are some odd (and very rare) circumstances where you might want to use And rather than AndAlso other than for bit manipulations.

    Another point is that CInt is going to throw an exception if the textbox is empty, or otherwise is not an integer. Using Integer.TryParse is the only safe way to perform the conversion if there is any chance that the textbox doesn't hold a number. If the text is entered by a user, then there is always a chance they will get it wrong, and if the text is not entered by a user, then a label would be better than a textbox. However, Integer.TryParse is not a drop-in replacement for CInt, so you'd have to look it up to see how it is different.
    My usual boring signature: Nothing

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

    Re: In between numbers. Help.

    Quote Originally Posted by Shaggy Hiker View Post
    However, Integer.TryParse is not a drop-in replacement for CInt, so you'd have to look it up to see how it is different.
    Or check out my code from post #3.
    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

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: In between numbers. Help.

    Yes, though you didn't use that technique in post #7, which is why I brought it up. The OP may well have overlooked #3 in favor of #7, as the latter was more recent and may have been more familiar.
    My usual boring signature: Nothing

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