Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is this Select Case statement not working properly? 5

Status
Not open for further replies.

RZillmer

Programmer
Nov 20, 2001
42
US
Code:
Sub Main()

    Dim classCode As Integer
    Dim otherValue as double

    classCode = 50

    Select Case classCode
        Case ((classCode >= 60) And (classCode <= 66))
            otherValue = 0.225
        Case ((classCode >= 50) And (classCode <= 59))
            otherValue = 0.09
        Case ((classCode < 50) Or (classCode > 66))
            otherValue = 0.12
    End Select

End Sub

Stepping through the code and hovering over the comparisons, I can see that it correctly evaluated the second Case to be true, yet it does not run the line underneath it. What is going on here?
 
Code:
Dim classCode As Integer
    Dim otherValue As Double

    classCode = 50

    Select Case classCode
        Case 60 To 66
            otherValue = 0.225
        Case 50 To 59
            otherValue = 0.09
        Case Else
            otherValue = 0.12
    End Select

good luck

If somethings hard to do, its not worth doing - Homer Simpson
 
works perfectly...I guess that's what I get for making it more complicated than it had to be!

Thanks
 
Do you now understand why ADoozer's code works and yours does not?
 
Code:
Select Case classCode

nb: classcode=50

I can see that it correctly evaluated the second Case to be true

or is this a test where your gonna teach us something totally baffling about the inner workings of VB

If somethings hard to do, its not worth doing - Homer Simpson
 
I'm always ready to learn! I give up...why? I thought about this for a while and the only thing I could come up with was something screwy in VB about integer comparison with <= or >=. That's my guess.
 
Because case uses VALUES, not expressions. Therefore the expressions are being evaluated to true or false, and then classCode is being compared to true or false.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
RZillmer,

If you prefer your intitial approach then use

Select case True

leaving everything else as it was.

vladk
 
Oh man...it's so obvious. I can't believe I did that. Thanks, all, for the refresher course!
 
It's not THAT obvious. Ask the experts here how many times THEY did the same thing. I'll bet most of them did it at least once.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top