|
-
May 22nd, 2013, 04:41 AM
#1
Thread Starter
Junior Member
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.
-
May 22nd, 2013, 05:58 AM
#2
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.
-
May 22nd, 2013, 06:00 AM
#3
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
-
May 22nd, 2013, 06:32 AM
#4
Thread Starter
Junior Member
Re: In between numbers. Help.
 Originally Posted by jmcilhinney
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.
-
May 22nd, 2013, 08:18 AM
#5
Re: In between numbers. Help.
 Originally Posted by tommyhoogstra
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
-
May 22nd, 2013, 09:27 AM
#6
Thread Starter
Junior Member
Re: In between numbers. Help.
 Originally Posted by dbasnett
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.
-
May 22nd, 2013, 09:45 AM
#7
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)
-
May 22nd, 2013, 04:42 PM
#8
Thread Starter
Junior Member
Re: In between numbers. Help.
 Originally Posted by jmcilhinney
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
-
May 23rd, 2013, 04:53 PM
#9
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
 
-
May 23rd, 2013, 07:56 PM
#10
Re: In between numbers. Help.
 Originally Posted by Shaggy Hiker
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.
-
May 24th, 2013, 10:33 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|