Did you know that the select case statement can use expressions? I sure didn't!
I knew that you could use several values, like this:[rbcode]
select case someInteger
case 1, 2, 3, 5, 8, 11
DoSomething
end select[/rbcode]
But you can use a range as well:[rbcode]
select case someInteger
case 1 to 5
DoSomething
end select[/rbcode]
And you can mix the two concepts![rbcode]
select case someInteger
case 1 to 5, 7, 11, 15
DoSomething
end select[/rbcode]
Then, to make matters even cooler, you can evaluate expressions using the Is clause![rbcode]
select case someInteger
case is > 42
DoSomething
case is <= 10
DoSomethingElse
end select[/rbcode]
I'm not certain when select case became so powerfully cool, but now that I know about it, I'll be using it!
I didn't know about "case is", but the others I did. Handy.
what would be the use of:
case 1 to 5, 7, 11, 15
Wouldn't this just suffice:
case 1 to 15
Maybe if you have multiple variables and you want to use the highest value ...
I've used the multiple values version for things before when I want to merge values. For instance:
select case whichPlatformVersion
case Win9x, Linux, Classic
DoOldStyleDrawing
case OSX
DoUglyMetalWindow
case Win2k to WinVista
DoYayWindowsMessage
case BeOS1 to BeOS5, Gentoo
DoRockinAwesomeOSDance
end select
In your example, 1 to 15 wouldn't work because you don't want 6, 8, 9, 10, 12, 13 or 14 in that case.
BeOS *was* rockin awesome. I wish it was updated, with multiuser and full POSIX support.
And it should have a big button on the BeBar (which is like the taskbar, but in BE!) and if you press the button, it has Superman fly you a beer. And it's a good beer, too, like Heinekin, and Superman offers to like get revenge on your enemies.
I wonder if you can use 'IsA', like:
Select Case Object
Case IsA MyClass
//Typecast as MyClass
Case IsA DifferentClass
//Typecast as DifferentClass
End Select
Ah, I love it when feature requests are filled. I sure missed that when I switched from VB to RB often. :D
@Charlie -- Nope, you can't use that; when I said "expressions", I meant just the shown examples. You can't have method calls or other operators in the case statement. But IsA would be a very handy addition that I think is fairly reasonable. I'd feature request that -- it's not a far leap from Is >= 42 to IsA FoobarClass.
"what would be the use of:
case 1 to 5, 7, 11, 15
Wouldn’t this just suffice:
case 1 to 15"
Those will give different results. The first will not trigger if the value is 6, 8-10, or 12-14, while the second will. The "is" and "to" keywords have no jurisdiction across commas.
Hmm... I missed the comment earlier--doh!
That is completely pimp! I had no idea Select was so cool! I use it all the time, but never knew about ANY of the versions other than the basic
case 1
case 2
case 3
I didn't even know about the IsA operator, though I've desired something which is exactly that. I had no idea I was so far behind!
Rock!!! I love this blog!
@Aaron - Feature Request:
Got hosed! Here: http://www.realsoftware.com/feedback/viewreport.php?reportid=jblukogs
It became that cool on August 2, 2003, according to my comments in the code. I'm not sure which version of RB that corresponds to - probably 5.5 :-)
and... uh... sure you can have method calls on a Case statement. Any full expression is valid. You can't use IsA in the fashion described for the simple reason that it's only half of an expression - it's missing the left hand side. (The "Is" keyword substitutes for the left-hand-side for all the comparison operators.)
Wait -- what? You can use method calls?!!? Holy crap, this is awesome! And you could say Is IsA PushButton?!??
Just to clarify:
You cannot say:
Select Case ob
Case isa Editfield then
...
End Select
You also cannot say:
Select Case ob
Case is isa Editfield then
...
End Select
However you CAN say
dim ob as object
ob= EditField1
select case true
case ob IsA editField
msgBox "editField"
case ob isA canvas
msgBox "canvas"
case ob isA listBox
msgBox "listBox"
else
msgBox "none of the above"
end select
Thanks to Ricardo Rojas in the forums for this tip & thanks to Aaron for turning me back onto Case.
- Stephen Dodd