Help with select case statement (1 Viewer)

jmonahan2009

New member
Local time
Today, 10:20
Joined
Oct 6, 2011
Messages
6
Hey guys. I'm taking a visual basic class in college, and I'm having trouble with some code.

Code:
Public Class Form1

    Private Sub btnFindLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindLocation.Click
        'Define variables

        Dim callNumber As Integer

        'Receive input

        callNumber = CInt(CallNumberBox.Text)

        'Find book

        Select Case callNumber
            Case 100 To 199
                OutputBox.Text = "The book is located in the basement."
            Case 200 To 500, Is > 900
                OutputBox.Text = "The book is located on the main floor."
            Case 500 To 900, [B]Is <> 700 to 750[/B]
                OutputBox.Text = "The book is located on the upper floor."
            Case 700 To 750
                OutputBox.Text = "The book is located in the archives."
            Case Else
                OutputBox.Text = "The book does not exist."

        End Select
    End Sub
End Class

I'm having trouble with the "Is <> 700 to 750" part. I'm pretty sure that the compiler stops reading it as one full statement, or what have you, after it reads the 700 part, so it doesn't see 700 to 750 as one full condition. I can't figure out how to get around this, any suggestions?
 

r.harrison

It'll be fine (I think!)
Local time
Today, 18:20
Joined
Oct 4, 2011
Messages
134
Try this:

Code:
Private Sub btnFindLocation_Click()

    'Define variables

        Dim callNumber As Integer

        'Receive input

        callNumber = CInt(CallNumberBox)

        'Find book

        Select Case callNumber
            Case 100 To 199
                OutputBox = "The book is located in the basement."
            Case 200 To 500, Is > 900
                OutputBox = "The book is located on the main floor."
            Case 500 To 900, Not 700 To 750
                OutputBox = "The book is located on the upper floor."
            Case 700 To 750
                OutputBox = "The book is located in the archives."
            Case Else
                OutputBox = "The book does not exist."

        End Select
End Sub
 

jmonahan2009

New member
Local time
Today, 10:20
Joined
Oct 6, 2011
Messages
6
Tried it. It compiles fine with the Not statement in there, but it still says anything between 700 and 750 is on the upper floor, which is incorrect.

I think I could change the case to something like 500 to 699, Is > 751 and it'd probably work, but I'd rather figure out how to do it this way just for the sake of knowing how to do it.
 

CBrighton

Surfing while working...
Local time
Today, 18:20
Joined
Nov 9, 2010
Messages
1,012
Aren't the criteria of Select Case statements checked in order?

i.e. if you put the 700 to 750 case before the 500 to 900 case then anything between 700 and 750 will have never get to the point of checking against 500 to 900?

:edit:

I don't really use Select Case much, but the comma between the ctireia can only mean "or" or "and".

In this row: Case 200 To 500, Is > 900
it looks like you are treating it as or. As in any number between 200 and 500 or any number over 900.

In this row: Case 500 To 900, Is <> 700 to 750
it looks like you are treating it as and. As in any number between 500 and 900 and not between 700 and 750.


Because I rarely use them I couldn't tell you which is right, but both cannot be.
 

jmonahan2009

New member
Local time
Today, 10:20
Joined
Oct 6, 2011
Messages
6
It does appear to work if I set the 700 to 750 case ahead of the 500 to 900. Thank you very much. Still a bit curious as to if it's possible to make it work the way I had it before though, lol.
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
Yes the statements are acted on in order, which also means that one of the 500 tests will not be actioned , suspect that it should be 200 to 499, and , does signify or

Brian
 

jmonahan2009

New member
Local time
Today, 10:20
Joined
Oct 6, 2011
Messages
6
Aren't the criteria of Select Case statements checked in order?

i.e. if you put the 700 to 750 case before the 500 to 900 case then anything between 700 and 750 will have never get to the point of checking against 500 to 900?

:edit:

I don't really use Select Case much, but the comma between the ctireia can only mean "or" or "and".

In this row: Case 200 To 500, Is > 900
it looks like you are treating it as or. As in any number between 200 and 500 or any number over 900.

In this row: Case 500 To 900, Is <> 700 to 750
it looks like you are treating it as and. As in any number between 500 and 900 and not between 700 and 750.


Because I rarely use them I couldn't tell you which is right, but both cannot be.

I think you're right. I tried changing the code to use and (both "and" and "&"), but it still wouldn't work though.
 

CBrighton

Surfing while working...
Local time
Today, 18:20
Joined
Nov 9, 2010
Messages
1,012
I've had a quick look online and I can see that , means or. But I don't see anyway to use and.

This has further re-enforced my use of If statements over Select case. It could do what you are using Select Case for and it will accept multiple criteria either as and or as or.
 

jmonahan2009

New member
Local time
Today, 10:20
Joined
Oct 6, 2011
Messages
6
I've had a quick look online and I can see that , means or. But I don't see anyway to use and.

This has further re-enforced my use of If statements over Select case. It could do what you are using Select Case for and it will accept multiple criteria either as and or as or.

Unfortunately I don't have that flexibility. The assignment says we have to use Case Select for this problem. But it's not really a problem, I can still complete it, I'll just have to change the order of the cases and play with the numbers a little bit. I just wanted to see if it was possible to do it the way I was trying, for the sake of knowing.
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
I've had a quick look online and I can see that , means or. But I don't see anyway to use and.

This has further re-enforced my use of If statements over Select case. It could do what you are using Select Case for and it will accept multiple criteria either as and or as or.

It's horses for courses, there are times when the simplicity of select case is far better than block Ifs and as you state here sometimes block Ifs are better
Brian
 

CBrighton

Surfing while working...
Local time
Today, 18:20
Joined
Nov 9, 2010
Messages
1,012
Is it "far better" purely for code clarity / style (things that only DB admins will see) or are there situations where using Select Case over If gives a measurable advantage to the end user?
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
I don't think end users give a damn what the coding is as long as the end product works reliably and effeciently.

Brian
 

stopher

AWF VIP
Local time
Today, 18:20
Joined
Feb 1, 2006
Messages
2,395
This works fine...

Code:
Public Class Form1

    Private Sub btnFindLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindLocation.Click
        'Define variables

        Dim callNumber As Integer

        'Receive input

        callNumber = CInt(CallNumberBox.Text)

        'Find book

        Select Case callNumber
            Case 100 To 199
                OutputBox.Text = "The book is located in the basement."
            Case 200 To 500, Is > 900
                OutputBox.Text = "The book is located on the main floor."
            Case 500 To 699, 751 To 900
                OutputBox.Text = "The book is located on the upper floor."
            Case 700 To 750
                OutputBox.Text = "The book is located in the archives."
            Case Else
                OutputBox.Text = "The book does not exist."

        End Select
    End Sub
End Class

Or of course you could put the 700 To 750 above the 500 To 900. But personally I think that's poor coding as someone reviewing the code in the future might not spot the significance of the order e.g. your tutor who is trying to mark your assignments.

Chris
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
In which case he should be sacked
However I agree that positive selection is better than selection by default.
Pity you left the 2 selections of 500 in there :D

Brian
 

CBrighton

Surfing while working...
Local time
Today, 18:20
Joined
Nov 9, 2010
Messages
1,012
I don't think end users give a damn what the coding is as long as the end product works reliably and effeciently.

Brian

That's why I was asking if there is any situation which gives a measurable benefit.

I never use them.

But if there are situations where it's provable that it's better to use them over an If statement then I'd like to know.
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
I don't know what you are looking for as provable, I think that it is upto you how you code the situation. If it is a list of simple cases I would always use the SelectCase, also I consider that each "Case" statement should be self contained and not rely on the order, whereas the Elseif of the blockIf implies the possibility of reliance on the previous statements, also as you noted earlier in the thread the case statements lack some of the options of the If, however in this thread it was not a true "and" situation but really a "but" , the correct solution was to select the correct ranges as shown by Stopher.

I noticed that the op never acknowledged the 500 situation I pointed out, but then he appeared to ignore post #6

Brian
 

CBrighton

Surfing while working...
Local time
Today, 18:20
Joined
Nov 9, 2010
Messages
1,012
I don't know what you are looking for as provable, I think that it is upto you how you code the situation.

I was just hoping to find out if there are any situations where it is better to use select case instead of if, provable or measuarable is purely to try to avoid hearsay.

If there's no measurable benefit then it can't be more efficient. If it were then you could measure things like the time saved, reduced memory use, etc.

If it's pure preference then that's fine. But if it's not and there are situations in my own work where I would be better to use select case then I'd rather find out now so I know in the future.
 

Brianwarnock

Retired
Local time
Today, 18:20
Joined
Jun 2, 2003
Messages
12,701
I've no idea whether there are any resource benefits either way, I would be surprised if it was noticeable in most situations but that is a pure guess. I have not discussed the options from that point of view, I am sorry if you think I have but I have reread my posts, I just believe that select case is simpler to code, read, and amend when it is a simple list, vertical and/or horizontal list, of values.

For what it is worth recently on another thread I persuaded a poster to use a block if instead of a select case.

Brian
 

Users who are viewing this thread

Top Bottom