Wish Select...Case on a range

ac9ts

Active Member
Licensed User
I've been working on some code that needs to sort measurements as doubles. I am forced to use a bunch of If..Then statements to figure out which bucket the value goes into. It works but it just looks bad. What would be nice is to use a Select...Case structure but I would have to call out every value. It sure would be nice if I could use:

B4X:
    Dim a As Double
    a=1.72341
   
    Select a
        Case 0 to 2
            Log ("A")
        Case 2 to 4
            Log ("B")
        Case Else
            Log ("C")
    End Select
...and have 1.72341 fall into bucket "A".
 

DonManfred

Expert
Licensed User
Wouldn't you still need to find an exact match using a list?
a simple list will not work in your case; you are right... But with a LIST of MAPs it should be easy possible to get the right one in one loop through the list of maps...
A map or a custom TYPE should work...

Not a solution. More my thoughts of creating the list
B4X:
    Dim checklist As List
    checklist.Initialize
    Dim m As Map = CreateMap("min": 0, "max": 2, "out": "A")
    checklist.Add(m)
    Dim m As Map = CreateMap("min": 2, "max": 4, "out": "B")
    checklist.Add(m)
    Dim m As Map = CreateMap("min": 4, "max": 6, "out": "C")
    checklist.Add(m)
And then a sub which will iterate through this list of maps to find the matching "out" for a given double
 

DonManfred

Expert
Licensed User
Something like
B4X:
Sub check(value As Double) As String
    Dim out As String=""
    For i = 0 To checklist.Size-1
        Dim m As Map = checklist.Get(i)
        If m.Get("min") < value AND m.Get("max") >= value Then
            Return m.Get("out")
        End If
    Next
End Sub
B4X:
    Log("1.234 -> "&check(1.234))
    Log("2.000 -> "&check(2))
    Log("3.400 -> "&check(3.4))
LogCat connected to: 9f1dbeed
--------- beginning of /dev/log/system
** Activity (main) Create, isFirst = true **
1.234 -> A
2.000 -> A
3.400 -> B
** Activity (main) Resume **
Wouldn't you still need to find an exact match using a list?
Yes, you need to run through the list of maps to find the matching one.. But this small sub is much better than 1000 IF THENs
 

ac9ts

Active Member
Licensed User
James, I swear I just thought of this exact answer seconds prior to opening your response.......great minds.....;)
 

ivanthomson

Member
Licensed User
You can use Select True
B4X:
    a=1.72341
  
    Select True
        Case a >= 0 AND a < 2
            Log ("A")
        Case a >= 2 AND a < 4
            Log ("B")
        Case Else
            Log ("C")
    End Select

Hey thanks for that trick with using the SELECT TRUE. That saved my butt where I need to do ranges on a select block.
 

krokiz

Member
Licensed User
James, I swear I just thought of this exact answer seconds prior to opening your response.......great minds.....;)
You can use Select True
B4X:
    a=1.72341
  
    Select True
        Case a >= 0 AND a < 2
            Log ("A")
        Case a >= 2 AND a < 4
            Log ("B")
        Case Else
            Log ("C")
    End Select
Man, I wan to THANK YOU immeasurably for this trick!!! I don't care this thread is rather old, I needed to express my feelings!
 
Top