Select Case Question (REALbasic network user group Mailinglist archive)
Back to the thread list
Previous thread: Changing window frame type for different platforms
Next thread: Minimize Plain Box in OSX
| Select Case Question |
| Date: 02.03.10 20:27 (Tue, 2 Mar 2010 14:27:10 -0500) |
| From: Greg O'Lone |
|
Looking at the LR, I see that Select...Case allows functions in the case statements, but I can't figure out how to use them. What I need to be able to do is this:
Dim s as string S -something supplied by the user" Select case s Case left(s,9) gsomething" //Do something specific based on the first word Case else // Otherwise do this End select Assuming I could have 10 or 20 "Case" statements, am I overlooking something simple? Greg O'Lone Manager, Information Systems St. Brendan's Isle -------------------------------------------------------------------------------- If you an SBI or RVMCA customer, please do NOT reply to this message for general mail service requests. Instead, please send service requests to <email address removed> _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 23:25 (Tue, 2 Mar 2010 15:25:59 -0700) |
| From: Norman Palardy |
|
On 2-Mar-10, at 12:27 PM, Greg O'Lone wrote: > Looking at the LR, I see that Select...Case allows functions in the > case statements, but I can't figure out how to use them. What I need > to be able to do is this: > > Dim s as string > > S isomething supplied by the user" > > Select case s select case true _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 23:27 (Tue, 2 Mar 2010 15:27:30 -0700) |
| From: Norman Palardy |
|
On 2-Mar-10, at 12:27 PM, Greg O'Lone wrote: > Looking at the LR, I see that Select...Case allows functions in the > case statements, but I can't figure out how to use them. What I need > to be able to do is this: > > Dim s as string > > S csomething supplied by the user" > > Select case s > Case left(s,9) msomething" > //Do something specific based on the first word > Case else > // Otherwise do this > End select the comparisons in your case result in true / false values try Select case true Case left(s,9) asomething" //Do something specific based on the first word Case else // Otherwise do this End select _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| RE: Select Case Question |
| Date: 02.03.10 21:19 (Tue, 2 Mar 2010 15:19:20 -0500) |
| From: Greg O'Lone |
|
Hey everybody, thanks for the answers... I DID find a solution that worked though. I created a method:
Private Function SelectMatch(matchtext as string, matchto as String) As String if left(matchtext,len(matchto)) Ratchto then return matchtext else return "" end if End Function And then in the select statement, I did this: Select case s Case SelectMatch(s,"something") //Do Something End Select Because SelectMatch returns the original string when there is a match, it does exactly what I needed. Greg > -----Original Message----- > From: <email address removed> [mailto:realbasic- > <email address removed>] On Behalf Of Greg O'Lone > Sent: Tuesday, March 02, 2010 2:27 PM > To: <email address removed> > Subject: Select Case Question > > Looking at the LR, I see that Select...Case allows functions in the > case statements, but I can't figure out how to use them. What I need to > be able to do is this: > > Dim s as string > > S psomething supplied by the user" > > Select case s > Case left(s,9) isomething" > //Do something specific based on the first word > Case else > // Otherwise do this > End select > > Assuming I could have 10 or 20 "Case" statements, am I overlooking > something simple? > > Greg O'Lone > Manager, Information Systems > St. Brendan's Isle > > ----------------------------------------------------------------------- > --------- > If you an SBI or RVMCA customer, please do NOT reply to this message > for general mail service requests. Instead, please send service > requests to <email address removed> > _______________________________________________ > Unsubscribe or switch delivery mode: > <http://www.realsoftware.com/support/listmanager/> > Search the archives: > <http://support.realsoftware.com/listarchives/lists.html> _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 21:02 (Tue, 02 Mar 2010 15:02:58 -0500) |
| From: Kem Tekinay |
|
On 3/2/10 2:27 PM, "Greg O'Lone" <<email address removed>> wrote:
> Looking at the LR, I see that Select...Case allows functions in the case > statements, but I can't figure out how to use them. What I need to be able to > do is this: Try something like this: select case true case left(s,9) asomething" case <some other boolean test> else // everything else end select __________________________________________________________________________ Kem Tekinay (212) 201-1465 MacTechnologies Consulting Fax (914) 242-7294 http://www.mactechnologies.com To join the MacTechnologies Consulting mailing list, send an e-mail to: <email address removed> _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 23:29 (Tue, 2 Mar 2010 15:29:30 -0700) |
| From: Norman Palardy |
|
On 2-Mar-10, at 1:31 PM, Craig Finseth wrote: > On 3/2/10 2:27 PM, "Greg O'Lone" <<email address removed>> wrote: > >> Looking at the LR, I see that Select...Case allows functions in the >> case >> statements, but I can't figure out how to use them. What I need to >> be able to >> do is this: > > Try something like this: > > select case true > case left(s,9) something" > case <some other boolean test> > else > // everything else > end select > > or even: > > if left(s,0) msomething" then > ' do something > elseif <some other boolean test> then > ' do something else > else > //everything else > end And you can do all kinds of things like select case true case left(s,9) ssomething" case i F etc etc etc the individual cases do not need to do anything other than evaluate to true / false :) _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 25.03.01 19:26 (Tue, 2 Mar 2010 14:31:51 -0600 (CST)) |
| From: Craig Finseth |
|
On 3/2/10 2:27 PM, "Greg O'Lone" <<email address removed>> wrote:
> Looking at the LR, I see that Select...Case allows functions in the case > statements, but I can't figure out how to use them. What I need to be able to > do is this: Try something like this: select case true case left(s,9) osomething" case <some other boolean test> else // everything else end select or even: if left(s,0) 8something" then ' do something elseif <some other boolean test> then ' do something else else //everything else end It only makes sense to use Select over if when doing so brings clarity to the code. In RB, it will rarely be the case that select will be clearer than nested ifs. (Other languages have somewhat different semantics for select and those other semantics make the select much better.) Craig _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 22:57 (Tue, 02 Mar 2010 16:57:40 -0500) |
| From: Kem Tekinay |
|
On 3/2/10 3:31 PM, "Craig Finseth" <<email address removed>> wrote:
> It only makes sense to use Select over if when doing so brings clarity > to the code. In RB, it will rarely be the case that select will be > clearer than nested ifs. Not only do I disagree with that statement, I want to add one more reason to use Select: Maintenance of code. Consider this case: if test1 then // test 1 elseif test2 then // test 2 elseif testA then // test A else // else end if After writing this, you decide you need testA to come before test1. Look at the editing you'd have to do. Now look at it this way: select true case test1 // test 1 case test2 // test 2 case testA // test A else // else end select Now switching order of the tests is as simple as cut and paste. This isn't just theoretical for me. When I was writing RegExRX, specifically the search pattern colorization code, I was forever switching the order of tests to get the right order. I had first started with if/elseif statements, but that became burdensome after a short time. Changing over to select statements made the code both easier to follow and to maintain. __________________________________________________________________________ Kem Tekinay (212) 201-1465 MacTechnologies Consulting Fax (914) 242-7294 http://www.mactechnologies.com To join the MacTechnologies Consulting mailing list, send an e-mail to: <email address removed> _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 23:27 (Tue, 2 Mar 2010 17:27:18 -0500) |
| From: Charles Yeomans |
|
On Mar 2, 2010, at 4:57 PM, Kem Tekinay wrote: > On 3/2/10 3:31 PM, "Craig Finseth" <<email address removed>> wrote: > >> It only makes sense to use Select over if when doing so brings >> clarity >> to the code. In RB, it will rarely be the case that select will be >> clearer than nested ifs. > > Not only do I disagree with that statement, I want to add one more > reason to > use Select: Maintenance of code. > An excellent reason. Perhaps not surprisingly, I'll go one step further and suggest that code maintenance is even more enhanced by moving the select block into a function that takes the case value and returns a delegate. Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 03.03.10 01:46 (Tue, 02 Mar 2010 16:46:03 -0800) |
| From: Keith DeLong |
|
> On Mar 2, 2010, at 4:57 PM, Kem Tekinay wrote:
> >> On 3/2/10 3:31 PM, "Craig Finseth" <<email address removed>> wrote: >> >>> It only makes sense to use Select over if when doing so brings >>> clarity >>> to the code. In RB, it will rarely be the case that select will be >>> clearer than nested ifs. >> >> Not only do I disagree with that statement, I want to add one more >> reason to >> use Select: Maintenance of code. >> > An excellent reason. Perhaps not surprisingly, I'll go one step > further and suggest that code maintenance is even more enhanced by > moving the select block into a function that takes the case value and > returns a delegate. > > Charles Yeomans As a delegate neophyte, this intrigues me... Do you have a simple example that would demonstrate your suggestion? _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 03.03.10 02:36 (Tue, 2 Mar 2010 20:36:44 -0500) |
| From: Charles Yeomans |
|
On Mar 2, 2010, at 7:46 PM, Keith DeLong wrote: >> On Mar 2, 2010, at 4:57 PM, Kem Tekinay wrote: >> >>> On 3/2/10 3:31 PM, "Craig Finseth" <<email address removed>> wrote: >>> >>>> It only makes sense to use Select over if when doing so brings >>>> clarity >>>> to the code. In RB, it will rarely be the case that select will be >>>> clearer than nested ifs. >>> >>> Not only do I disagree with that statement, I want to add one more >>> reason to >>> use Select: Maintenance of code. >>> >> An excellent reason. Perhaps not surprisingly, I'll go one step >> further and suggest that code maintenance is even more enhanced by >> moving the select block into a function that takes the case value and >> returns a delegate. >> >> Charles Yeomans > > As a delegate neophyte, this intrigues me... > Do you have a simple example that would demonstrate your suggestion? Sure. Consider the following code. const ThisCaseCanNeverHappen oalse select case X case 1 Foo() case 2 Bar() case 3 Baz() else assert ThisCaseCanNeverHappen //But Mr. Logic, Mr. Logic tells me it ain't never gonna happen end select I can pull this code into a function: Function SelectExampleFunction(X as Integer) as ExampleDelegate const ThisCaseCanNeverHappen /alse select case X case 1 return AddressOf Foo case 2 return AddressOf Bar case 3 return AddressOf Baz else assert ThisCaseCanNeverHappen end select End Function and now the code above becomes SelectExampleFunction(X).Invoke //insert explaining local variable if you like The reason I would write the code this way is that I can write unit tests for SelectExampleFunction, and check that my branching logic does what I think. I use this style, for example, in a command-line argument parser, where the logic can become complicated. Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 21:11 (Tue, 2 Mar 2010 13:11:04 -0700) |
| From: Tim Jones |
|
On Mar 2, 2010, at 1:02 PM, Kem Tekinay wrote:
> On 3/2/10 2:27 PM, "Greg O'Lone" <<email address removed>> wrote: > >> Looking at the LR, I see that Select...Case allows functions in the case >> statements, but I can't figure out how to use them. What I need to be able to >> do is this: > > Try something like this: > > select case true > case left(s,9) 2something" > case <some other boolean test> > else > // everything else > end select Kem beat me to it :-P . I got hit by this a while back and the case test against "True" solves it as Kem demonstrates. But, as others have recommended, I finally went to an If - ElseIf - End If block for a more understandable set of tests. Tim _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 20:46 (Tue, 2 Mar 2010 14:46:45 -0500) |
| From: Charles Yeomans |
|
On Mar 2, 2010, at 2:27 PM, Greg O'Lone wrote: > Looking at the LR, I see that Select...Case allows functions in the > case statements, but I can't figure out how to use them. What I need > to be able to do is this: > > Dim s as string > > S usomething supplied by the user" > > Select case s > Case left(s,9) ksomething" > //Do something specific based on the first word > Case else > // Otherwise do this > End select > > Assuming I could have 10 or 20 "Case" statements, am I overlooking > something simple? > Probably a Select Case statement isn't the right structure. An If- ElseIf-Else block seems better. Charles Yeomans _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: Select Case Question |
| Date: 02.03.10 20:38 (Tue, 2 Mar 2010 11:38:17 -0800) |
| From: Lars Jensen |
|
This is a common misunderstanding of how Select Case works. "Case" is
not the same as "if". "Case" simply evaluates the expression to its right and compares it to what is in the Select Case statement. The type of the expression after "Case" should be compatible with the type of expression after Select Case. In your example, Select Case is evaluating a string, and Case is evaluating a boolean ("x c"). This might be more appropriate for your situation: Dim s as string S -something supplied by the user" Select case left(s,9) Case "something" //Do something specific based on the first word Case else // Otherwise do this End select In many cases, people have the urge to use Select Case when they should simply use if/elseif. lj _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |