Select Case - What's going on???

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

I cut the following code out of a module I was following in the debugger.
When li_LevelDifference = 0, the control goes to the first Case,
li_LevelDifference < 0. How come?????

I Compacted and Repaired a couple of times.

Dim li_LevelDifference as Integer

li_LevelDifference = li_level2 - li_level1

Select Case li_LevelDifference

Case li_LevelDifference < 0
fncLevelTrend = "-"
Case li_LevelDifference > 0
fncLevelTrend = "+"
Case li_LevelDifference = 0
li_DaysDifference = li_days2 - li_days1

End Select
 
In
Laurel said:
I cut the following code out of a module I was following in the
debugger. When li_LevelDifference = 0, the control goes to the first
Case, li_LevelDifference < 0. How come?????

I Compacted and Repaired a couple of times.

Dim li_LevelDifference as Integer

li_LevelDifference = li_level2 - li_level1

Select Case li_LevelDifference

Case li_LevelDifference < 0
fncLevelTrend = "-"
Case li_LevelDifference > 0
fncLevelTrend = "+"
Case li_LevelDifference = 0
li_DaysDifference = li_days2 - li_days1

End Select

When you say the control "goes to the first Case", do you mean that it
highlights
Case li_LevelDifference < 0

or that it actually goes on to highlight and execute
fncLevelTrend = "-"

? I expect the former, because it has to evaluate the condition, but
not the latter.
 
I fixed this by changing the syntax to Case Is < 0.

But how come I didn't get an error if my syntax was incorrect? Still
curious about what was happening.
 
In
Laurel said:
I fixed this by changing the syntax to Case Is < 0.

But how come I didn't get an error if my syntax was incorrect? Still
curious about what was happening.

Oh, now I see. I didn't even notice that you had mixed up your syntax.
Your Select Case statement, as originally written, will be comparing 0
(the value of li_LevelDifference) with the result of the logical
expression (li_LevelDifference < 0), which will be False, which will be
equivalent to 0. So the evaluation comes out to a test of whether 0 =
0, and so that case will be executed.
 
Aha!!!!
Thanks for answering both my questions.
1 - Why did it do this?
2 - How come "bad" syntax wasn't flagged. But it wasn't bad syntax.

Thanks again!
 
Like you say, your syntax was fine, but your logic was flawed/mistated.

Select Case True
Case li_LevelDifference < 0
(etc)

Would work with your original Case syntax. Case phrasing depends on what you
are testing.

HTH,
 
Back
Top