05-05-2025, 07:30 PM
Note that I titled this a SELECT CASE Irregularity. That's not exactly the case here, as all I'm doing is highlighting something that has apparently been a part of SELECT CASE for the last 30 years or more.
Keep in mind:
1) Nothing has changed.
2) Nothing is broken.
3) We won't be changing this behavior as it's the EXACT behavior QB45 produces and it's our holy grail of compatibility for all these old commands.
That said, everyone should give this next little code snippet a quick run and see the wild results for themselves before I go on to explain what's happening here:
Now, did you run that?
If not, run that before you continue reading. Go on. It'll take all of 30 seconds to copy/paste and run. See if any of you guys can sort out what's going on here, and see if these are the results you were expecting.
Okies, I'm going to assume you guys tried the above at this point.
ISN'T THE RESULT THERE COMPLETELY MIND BOGGLING??!!
i = x, but x <> i
Now.... how the BLEEP does that make even the slightest bit of sense???
.
.
.
Any clue?
.
.
.
.
Apparently, when dealing with SELECT CASE, the comparison type is cast to match the case type!!
Again, keep in mind that this isn't new behavior; it's the exact same thing that we've seen for the past thirty years. But... WHO KNEW??!!
What's going on here is basically:
SELECT CASE i <--- i is an INTEGER
CASE IS < x <--- here, x is a float so its value is cast to an INTEGER for compare. It's basically the same as CINT(x). A float value of 1.1 ends up being processed as if it was simply 1.
And that conversion from float to integer with X means that the values are always going to match perfectly.
.
.
.
And with the second part?
We see the same thing happening:
SELECT CASE x <--- here, x is a SINGLE
CASE is > i <--- here, i is an INTEGER, so we cast it to a FLOAT. Of course an integer value of 1 is also the exact same float value of 1. We're not changing the value here of anything, so we're comparing the full value of x to i, without any rounding occurring.
Wild. Right?
Just keep in mind, this is the way it's always been. You've been coding and having this happen for years and years, and either didn't notice, or weren't affected by it in any sort of meaningful way.
But it *IS* something to keep in mind moving forward, to make certain that you're getting the results and comparisons which you want. I'd stumbled across this while playing around and testing things with the CINT, CLNG, _ROUND topic, and it blew my mind that I'd never ever noticed this strange little quirk before.
It's always been here. Always been doing things this way. But I certainly didn't know about it, and I'm willing to be a vast majority of our user base didn't know either.
SELECT CASE casts the CASE type to match the SELECT CASE type.
Be aware of that moving forward guys. It may save you some serious head scratching and debugging sometime in the future.
Keep in mind:
1) Nothing has changed.
2) Nothing is broken.
3) We won't be changing this behavior as it's the EXACT behavior QB45 produces and it's our holy grail of compatibility for all these old commands.
That said, everyone should give this next little code snippet a quick run and see the wild results for themselves before I go on to explain what's happening here:
Code: (Select All)
Dim i As Integer
For x = 0 To 2 Step .1
i = x
Print x, i,
Select Case i
Case Is < x: Print "Round Down",
Case Is = x: Print "Equal",
Case Is > x: Print "Round up",
End Select
Select Case x
Case Is > i: Print "Round Down"
Case Is = i: Print "Equal"
Case Is < i: Print "Round up"
End Select
Next
Now, did you run that?
If not, run that before you continue reading. Go on. It'll take all of 30 seconds to copy/paste and run. See if any of you guys can sort out what's going on here, and see if these are the results you were expecting.
Okies, I'm going to assume you guys tried the above at this point.
ISN'T THE RESULT THERE COMPLETELY MIND BOGGLING??!!
i = x, but x <> i
Now.... how the BLEEP does that make even the slightest bit of sense???
.
.
.
Any clue?
.
.
.
.
Apparently, when dealing with SELECT CASE, the comparison type is cast to match the case type!!
Again, keep in mind that this isn't new behavior; it's the exact same thing that we've seen for the past thirty years. But... WHO KNEW??!!
What's going on here is basically:
SELECT CASE i <--- i is an INTEGER
CASE IS < x <--- here, x is a float so its value is cast to an INTEGER for compare. It's basically the same as CINT(x). A float value of 1.1 ends up being processed as if it was simply 1.
And that conversion from float to integer with X means that the values are always going to match perfectly.
.
.
.
And with the second part?
We see the same thing happening:
SELECT CASE x <--- here, x is a SINGLE
CASE is > i <--- here, i is an INTEGER, so we cast it to a FLOAT. Of course an integer value of 1 is also the exact same float value of 1. We're not changing the value here of anything, so we're comparing the full value of x to i, without any rounding occurring.
Wild. Right?
Just keep in mind, this is the way it's always been. You've been coding and having this happen for years and years, and either didn't notice, or weren't affected by it in any sort of meaningful way.
But it *IS* something to keep in mind moving forward, to make certain that you're getting the results and comparisons which you want. I'd stumbled across this while playing around and testing things with the CINT, CLNG, _ROUND topic, and it blew my mind that I'd never ever noticed this strange little quirk before.
It's always been here. Always been doing things this way. But I certainly didn't know about it, and I'm willing to be a vast majority of our user base didn't know either.
SELECT CASE casts the CASE type to match the SELECT CASE type.
Be aware of that moving forward guys. It may save you some serious head scratching and debugging sometime in the future.
