Xojo string comparisons are case insensitive by default. Isn’t a Select Case bug, the same will happen with this code:
Var st As String = "m"
If st = "M" Then
MessageBox("wrong")
ElseIf st = "m" Then
MessageBox("right")
End If
For case sensitive string comparisons, you can use String.Compare. This will work with your case sensitive example:
Var st as string = "m"
Select Case 0
Case st.Compare("M", ComparisonOptions.CaseSensitive)
msgbox ("wrong")
Case st.Compare("m", ComparisonOptions.CaseSensitive)
msgbox ("right")
End Select
Thanks, Ricardo. In my code, it looks like ComparisonOptions.CaseSensitive is needed almost everywhere where alphanumeric comparisons are made. But, for this one-letter Select Case, this is an easier workaround for me, especially as most of the work can be done with a simple regex applied to the existing code:
var st as string = "m"
select case asc(st)
case asc("M")
msgbox ("wrong")
case asc("m")
msgbox ("right")
end select
Thanks, Tim. Did not notice the RegExOptions. This makes me wonder: since there is a Match Case option for RegEx, why isn’t there one for regular code?
This is a kind of a mess in the language, it causes even unknown bugs. I think that Xojo needs a set of strict operators (case sensitive), as an inspiration maybe could be the opposed of those used by some IBM products (e.g. ObjectServer SQL), where = > < etc are case sensitive and the prefix “%” make them insensitive.
For Xojo it would be the other way around as:
"a" = "A" // True
"a" = "a" // True
"a" %= "A" // False, case sensitive equal
"a" %= "a" // True
"a" > "A" // False
"a" > "a" // False
"a" %> "A" // True, case sensitive "greater than"
"a" %> "a" // False
So a code like this would work:
Var st As String = "m"
Select Case st
Case %"M" // same as {Case Is %= "M"} // Consider binary string
msgbox ("wrong")
Case %"m" // same as {Case Is %= "m"} // Consider binary string
msgbox ("right")
Case Is %< "a"
// whatever
Case Is %< "A"
// Whatever
End Select
Simple is, obvious no, or one guy (and many others) would not classify the behavior that he found (because most languages do not behave this way) as a bug.
What you do with ASC() is called workaround. As I said, it would be better if the language had proper (direct and faster) support for case sensitive content right in the language (like operators and literals).
Var st As String = "m"
Select Case st
Case "M","m"
MsgBox ("M or m")
Case Else
MsgBox ("Else")
End Select
Select Case st.Lowercase
Case "m"
MsgBox ("m")
Case Else
MsgBox ("Else")
End Select