B4J Question Using To in Select Cases?

Hi,

I am attempting to use a reserved word that doesn't seem to exist in B4x, the To keyword, in this context:

B4X:
Select Case SomeChar
 Case "A" To "Z", "a" To "z"
  ' Identifies a keyword

End Select

But in this case it the compiler throws errors if I attempt to do anything but this:
B4X:
Select Case SomeChar
 Case "A", "B","C", ... "Z", "a", "b", "c", ... "z"
  ' Identifies a keyword

End Select

What is an alternative to this that I can use? I would assume that it would have to be something like RegEx or something like that... but I'm unsure how to use it like the To keyword for a specific case?

Thanks,
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Don't you think "ranges" needs a little class?

B4X:
    Private ranges As rangeObject        'a small standard class with 50 lines of code
    ranges.Initialize(.00000001)        'to define equality of double precision numbers
    
    'For string ranges, comma and ~ are reserved characters (as legibility tradeoff) all values are trim(med)
    Log(ranges.Set("a~c, da~dz, cat").Contains("db"))        'true
    Log(ranges.Set("a~c, da~dz, cat").Contains("cat"))        'true
    Log(ranges.Set("a~c, da~dz, cat").Contains("d"))        'false
    
    'For numeric ranges, min and max are -infinity and infinity
    Log(ranges.Set("min~40, 100~200, 500~max, 42").Contains(-.25))            'true
    Log(ranges.Set("min~40, 100~200, 500~max, 42").Contains(42))            'true
    Log(ranges.Set("min~40, 100~200, 500~max, 42").Contains(43))            'false

B4X:
Sub Class_Globals
    Private fx As JFX
    Private ranges As List
    Private tolerance As Double
End Sub

Public Sub Initialize(tolerance_ As Double)
    tolerance = tolerance_
    ranges.Initialize
End Sub

Public Sub Set(definition As String) As rangeObject
    ranges.clear
    Dim v() As String = Regex.Split(",", definition)
    For Each s As String In v
        ranges.Add(s.trim)       
    Next
    Return Me
End Sub

Public Sub Contains(comparator As Object) As Boolean
    If comparator Is String Then
        Return checkStrings(comparator)
    Else if IsNumber(comparator) Then
        Return checkNumbers(comparator)
    End If
    Return False
End Sub

Private Sub checkNumbers(comparator As Double) As Boolean
    For Each s As String In ranges
        Dim w() As String = Regex.Split("~", s)
        If w.Length = 2 Then
            Dim lowest, highest As Double
            If w(0).toLowerCase = "min" Then lowest = -1/0 Else lowest = w(0)
            If w(1).toLowerCase = "max" Then highest = 1/0 Else highest = w(1)
            If comparator >= lowest And comparator <= highest Then Return True
        Else
            If Abs(w(0).As(Float) - comparator) < tolerance Then Return True
        End If
    Next
    Return False
End Sub

Private Sub checkStrings(comparator As String) As Boolean
    For Each s As String In ranges
        Dim w() As String = Regex.Split("~", s)
        If w.Length = 2 Then
            If w(0).Trim.CompareTo(comparator.trim) <=0 And w(1).Trim.CompareTo(comparator.trim) >=0 Then Return True
        Else
            If w(0) = comparator Then Return True
        End If
    Next
    Return False
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi,

I am attempting to use a reserved word that doesn't seem to exist in B4x, the To keyword, in this context:

B4X:
Select Case SomeChar
 Case "A" To "Z", "a" To "z"
  ' Identifies a keyword

End Select

But in this case it the compiler throws errors if I attempt to do anything but this:
B4X:
Select Case SomeChar
 Case "A", "B","C", ... "Z", "a", "b", "c", ... "z"
  ' Identifies a keyword

End Select

What is an alternative to this that I can use? I would assume that it would have to be something like RegEx or something like that... but I'm unsure how to use it like the To keyword for a specific case?

Thanks,
i am not sure i understood 100% what you want to do but this could work:

B4X:
SomeChar = "a" 
 Dim alowedChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Select alowedChars.Contains(SomeChar.ToUpperCase)
        Case True
            Log(True)
        Case False
            Log(False)
    End Select
 
Upvote 0
Top