tags:

views:

522

answers:

7

i need to check whether a demical is 0 through 49.99 or 50 through 99.99 or 100 through 199.99 or greater than 200. i am trying to do this with select case, but i am not sure of the syntax. please help!

+1  A: 

Why don't you try if/then/else? They are equivalent, and I am not sure if case statement in VBasic can handle non-integers values.

eKek0
if/then/else can be compiled differently from a select case statement. Many compilers optimize the select case statement.
But at least for old BASIC languages and Visual Basic, select..case is MUCH faster than if, don't know how it is for VB.NET
schnaader
case is faster than tons of if/elseif/elseif/else
Fredou
I believe the same still applies to.NET schnaader.
+2  A: 
    Select Case aa
        Case 1 To 1.49
            MsgBox(1)
        Case 1.5 To 2
            MsgBox(2)
        Case Else
            MsgBox("was lower than 1 or higher than 2 or between 1.49 and 1.5")
    End Select

this(below) would go into case else

   Dim aa As Double = 1.499

this(below) will go into case 1 to 1.49

   Dim aa As Double = 1.4

this(below) will go into case 1.5 to 2

   Dim aa As Double = 1.78

other way of doing it: From here

    Select Case value
        Case Is <= 49.99
            Debug.WriteLine("first group")
        Case Is <= 99.99
            Debug.WriteLine("second group")
        Case Is <= 199.99
            Debug.WriteLine("third group")
        Case Else
            Debug.WriteLine("fourth group")
    End Select

and maybe this too:

    Select Case true
        Case (value >= 0 andalso value <= 49.99)
            Debug.WriteLine("first group")
        Case (value >= 50 andalso value <= 99.99)
            Debug.WriteLine("second group")
        Case (value >= 100 andalso value <= 199.99)
            Debug.WriteLine("third group")
        Case Else
            Debug.WriteLine("fourth group")
    End Select
Fredou
when you say 1 to 2, does it include 1 and 2?
I__
include anything between 1 and 2
Fredou
See jvanderh's answer for something that's even closer to your question.
Joel Coehoorn
I agree with you Joel
Fredou
what people think about the select case true?
Fredou
+2  A: 
Dim range as Integer
range = someInteger / 50
'So, if range = 0 is 0-49.99, if it's 1 it's 50 to 99.99, etc
AlbertEin
VB.Net doesn't do integer division by default: it rounds.
Joel Coehoorn
+4  A: 
 Dim value As Double = 133.5
        Select Case value
            Case Is <= 49.99
                Debug.WriteLine("first group")
            Case Is <= 99.99
                Debug.WriteLine("second group")
            Case Is <= 199.99
                Debug.WriteLine("third group")
            Case Else
                Debug.WriteLine("fourth group")
        End Select

Where do values as 49.992 fall in your question? Since you said 0-49.99 and then 50-99.99 anything between 49.99 and 50 where does it go? In my example above it would be included in one of the options so it is values between 0 and 49.99, values between 49.99 and 99.99, etc, etc.

jvanderh
Beat me to it - this showed up while I was still composing ;)
Joel Coehoorn
I think your answer is better than mine so since mine is marked as accepted, do you allow me to put your into mine?
Fredou
If it is better or not depends on the premise of the question which isn't clear. If the intent is to ignore values between 49.99 and 50 then yours is correct. You can copy mine and add it to yours as an option with that clarification.
jvanderh
heh I composed my answer then saw this. It's incredible how fast answers show up. Us vision impaired though don't have the ability to see things magically appear on the page through AJAX.
This is a duplicate answer resulting from an answer coming in while I was composing, please delete.
Sorry, wrong comment box.
jvanderh: You say "values between 0 and 50 (not including 50), values between 50 and 100 (not including 100), etc." but really in your example you're dealing with values between 0 and 49.99, between 49.99 and 99.99, etc. -- i.e., the number 49.991 would fall into the supposedly "between 50 and 100" category, which is clearly wrong. I've offered a slightly modified version of your answer using the '<' operator instead of '<='.
Dan Tao
You are right Dan, I edited the answer and it doesn't match the comment. The idea was to get the person asking the question to clarify his question.
jvanderh
+3  A: 

AlbertEin is onto something, but to do integer division like that in VB.Net you have to write it like this:

Dim range as Integer
range = someInteger \ 50

Notice the backwards division symbol. From there you can Select Case range.

But really, jvanderh's answer most expresses what you want to do, because it allows for easy addition of cases in the future that don't break on a multiple of 50 and don't require future maintainers to follow the math or know about the \ operator.

Joel Coehoorn
+1  A: 

This is how I would do it, I use the # to explicitly state the values are of type "double".

   Dim input As Double = 2.99

    Select Case input
        Case 0.0# To 49.99#
            Response.Write("Between 0 to 49.99")
        Case 50.0# To 99.99#
            Response.Write("Between 50 and 99.99")
        Case Else
            Response.Write("The value did not fall into a range.")
    End Select
Zachary
I think you mean 'd' rather than '#'
Joel Coehoorn
+1  A: 

I have my doubts that you've framed this question to say exactly what you mean. Do you really want the first group to encompass just 0 through 49.99? Or do you really mean 0 up to but not including 50, and you simply expect your input to have 2 decimal places or fewer? If you want to group numbers by fifties, say, then it is very strange to write:

Select Case value
    Case Is <= 49.99
        Debug.WriteLine("49.99 or less")
    Case Is <= 99.99
        Debug.WriteLine("greater than 49.99, 99.99 or less")
    ' ... and so on '
End Select

The number 49.995 here falls into the second group, which seems counterintuitive. Picking two decimal places as the cut-off point is arbitrary.

The '<=' operator is not the way to go here; use the '<' operator; it makes a lot more sense:

Select Case value
    Case Is < 50
        Debug.WriteLine("less than fifty")
    Case Is < 100
        Debug.WriteLine("fifty or greater, less than 100")
    ' ... and so on '
End Select
Dan Tao