Select Case evaluation

Post

Posted
Rating:
#1 (In Topic #1347)
Regular
chrisRoald is in the usergroup ‘Regular’
Hi, I've coded a function (see below) with a sequence of Cases in a Select Case > End Select statement.
However, when my code runs, the first Case is matched and processes the contained statements - as expected - but then proceeds straight to End Select instead of dropping down to the next Case!  How to I get the code to evaluate each Case in succession as described in the documentation
(/lang/select - Gambas Documentation)?
(The msExists function call should return True in each Case statement, so processing all the code.)

Gambas 3.19.3

Code (gambas)

  1. Public Function applySetupFile(Setup As Collection, entityX As String, typeX As String) As Integer
  2.    
  3.     Dim template As Collection
  4.     Dim entity As Collection
  5.     Dim type As Collection
  6.     Dim metaName As String
  7.     Dim parameters As Object[]
  8.     Dim specified As integer
  9.    
  10.         Case Setup.Exist("metaTemplate")
  11.             template = Setup["metaTemplate"]
  12.             metaName = template["metaName"]
  13.             specified = 1
  14.  
  15.         Case msExists(template, entityX, ByRef entity)
  16.             Inc specified
  17.            
  18.         Case msExists(entity, typeX, ByRef parameters)
  19.             $handler.listSetupParameters(parameters, "")
  20.             Inc specified
  21.            
  22.     End Select
  23.     Return specified
  24.  
  25.  
  26.  
Thanks,
     chrisRoald
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 That is how it is designed to work.
If you want to test for multiple conditions then Select is not what you want.
AFAICS what you appear to need is just a successive set of If's.

b
p.s. I don't see how you got that idea from the indicated help page, what was unclear?
p.p.s Select Case True is not sensible. It will always be true, as in If True=True then …

Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
You can't , that's not how Select works.

It Selects the first True condition.

Use If blocks , Select case is like using If and Else if
When it finds a condition to be true it will only run that bit of code and ignore the rest.
Like it Selected it.

Code (gambas)

  1.  
  2.         If Setup.Exist("metaTemplate") Then
  3.             template = Setup["metaTemplate"]
  4.             metaName = template["metaName"]
  5.             specified = 1
  6.       Endif
  7.  
  8.         If msExists(template, entityX, ByRef entity) Then Inc specified
  9.              
  10.         If msExists(entity, typeX, ByRef parameters) Then
  11.             $handler.listSetupParameters(parameters, "")
  12.             Inc specified
  13.         Endif
  14.  
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

chrisRoald said

Hi, I've coded a function (see below) with a sequence of Cases in a Select Case > End Select statement.
However, when my code runs, the first Case is matched and processes the contained statements - as expected - but then proceeds straight to End Select instead of dropping down to the next Case!  How to I get the code to evaluate each Case in succession as described in the documentation
(/lang/select - Gambas Documentation)?

I changed the first line of the wiki page

from this..
Selects an expression to compare, and execute the code enclosed in the corresponding matching CASE statement.

Selects one particular condition from a group, and executes only the code enclosed in the corresponding matching 'CASE' statement.

So it's clearer that the "Select" function in mono-conditional and woks as a Selector for a single Case.

PS. Drop the Case word from the Select Line, it's just confusing.

So it's like this…

Code (gambas)

  1.  
  2. Select iMyInteger
  3.   Case 0 To 9
  4.     Print "It's less than 10"
  5.   Case 10, 20, 30, 40
  6.     Print "It's divisible by 10"
  7.     Print "It's something else"
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
chrisRoald is in the usergroup ‘Regular’
hi All, thanks for the correction of my assumptions :roll: , and the useful tips.
The most misleading bit of the wiki document for Select is shown in bold:-
_____________________________
Note
The TO keyword can also be used without a lower bound which makes the case like a less than range. For example.
  CASE TO 0
will act as CASE < 0 and since the cases are evaluated in succession

chrisRoald
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
It would be even better  if it said "the first case encountered that evaluates true ignoring the remaining" or words to that effect.

Consider

Code (gambas)

  1. dim x = 7
  2. select x \ 2
  3.   case to 2
  4.     do that
  5.   case to 1
  6.     do the_other
  7.   case > 1
  8.     do whatever
  9.     fall over
  10.  
In that case (sic) it will do whatever.
Also consider what happens if x = -7 and we change the Select expression to

Code (gambas)

  1. Select Abs(x \ 2)
!

Online now: No Back to the top
1 guest and 0 members have just viewed this.