B4J Question (Solved) Convert If-ElseIf to Select-Case ?

aeric

Expert
Licensed User
Longtime User
I prefer to use Select-Case compare to If-Else but I can't think how to convert the following code:
B4X:
    If aString.StartsWith("A") Then
       
    Else If aString.StartsWith("B") Then
       
    Else If aString.StartsWith("C") Then
       
    Else If aString.StartsWith("D") Then
       
    Else
       
    End If
Is it possible to use Select-Case in this case?
 
Last edited:
Solution
Select True

Case aString.StartsWith("A")
....

Case aString.StartsWith("B")
...

End Select

There's probably a better way - thats the beauty of coding :)

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim aString As String = "test"
    Dim value As String = aString.SubString2(0,1).ToUpperCase
    Select value
    Case "T"
        ' Bla
    End Select
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
    Dim aString As String = "test"
    Dim value As String = aString.SubString2(0,1).ToUpperCase
    Select value
    Case "T"
        ' Bla
    End Select
But the value may not length of 1. It can be different length.

To be more specific:
B4X:
If SubName.StartsWith("Get") Then

Else If SubName.StartsWith("Post") Then

Else If SubName.StartsWith("Put") Then

Else If SubName.StartsWith("Delete") Then

Else

End If
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Select True

Case aString.StartsWith("A")
....

Case aString.StartsWith("B")
...

End Select

There's probably a better way - thats the beauty of coding :)

This is what I would do and have done so so so many times.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
That's creative. I didn't expect Case to support an expression as Java Switch only accepts values, but having looked at the generated code Erel does more transformation than I expected to get it to do so. Previously I just assumed, wrongly, that it would directly mirror Java.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Since we all feel creative today, here's another one:
B4X:
dim srclist as string ="Get Post Put Delete List "   'your list of candidate words
dim teststring as string = "Post "                           'the word you're looking for; space used to terminare a word
dim idx as int = srclist.IndexOf(teststring)
Select idx
  Case -1
     'not found
  Case 0
    'Get
  Case 4
    'Post
  '.....
end select

And any variation of it.. :)
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Since we all feel creative today, here's another one:
B4X:
dim srclist as string ="Get Post Put Delete List "   'your list of candidate words
dim teststring as string = "Post "                           'the word you're looking for; space used to terminare a word
dim idx as int = srclist.IndexOf(teststring)
Select idx
  Case -1
     'not found
  Case 0
    'Get
  Case 4
    'Post
  '.....
end select

And any variation of it.. :)
I did 20 years ago on a pjt of mine???
 
Upvote 0

emexes

Expert
Licensed User
here's another one
Close ? but no cigar. Mission was to Select based on the start of aString, not the entire aString. Plus there is no guarantee (i) that the separator after the start of aString is a space (although could reasonably expect it to be non-alpha/numeric), or (ii) that there is even a separator at all (although this can be worked-around by temporarily appending a separator to aString).
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Mission was to Select based on the start of aString, not the entire aString.
I missed that requirement :(
Reading the items in the example list (post #3) they reminded me of some FTP commands, so whole strings.

Separator: the searched for string doesn't need a separator. It's used to make the "available items" list easier to read (and to avoid situations like Post/Postman).
Anyway it was just an old trick as @tigrot reminded me.

BTW, if the OP really needs to test whether aString begins with A, B,C.., he could
- extract the ASCII code from the first char of the searched for string (eventually normalized to 0..25)
- use the code as the value for the select statement
as suggested in post #2
or look for that char in the "alphabet" string ("ABCDEF..") and use its position as the case value.

Tonight I don't feel so creative so I stop here :)
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
But the value may not length of 1. It can be different length.

To be more specific:
B4X:
If SubName.StartsWith("Get") Then

Else If SubName.StartsWith("Post") Then

Else If SubName.StartsWith("Put") Then

Else If SubName.StartsWith("Delete") Then

Else

End If


B4X:
Dim aString As String = "test"
    Dim value As String = aString.SubString2(0,3).ToUpperCase
    Select value
        Case "Get"
            ' Get
        Case "Pos"
            ' PosT
        Case "Put"
            ' Put
        Case "Del"
            ' Delete
    End Select
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i would have done it like this

B4X:
Sub checkFirstLetter
    Dim l As List = Array As String("ben","avi","dani","charlie")
    For Each name As String In l
        Select True
            Case startwith(name,"A")
                Log($"0: ${name}"$)
            Case startwith(name,"B")
                Log($"1: ${name}"$)
            Case startwith(name,"C")
                Log($"2: ${name}"$)
            Case startwith(name,"D")
                Log($"3: ${name}"$)
            Case Else
                Log("not found")
        End Select
    Next
End Sub

Sub startwith(name As String, c As Char) As Boolean
    Return name.ToUpperCase.StartsWith(c)
End Sub

logs:

Waiting for debugger to connect...
Program started.
1: ben
0: avi
3: dani
2: charlie
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
i would have done it like this

B4X:
Sub checkFirstLetter
    Dim l As List = Array As String("ben","avi","dani","charlie")
    For Each name As String In l
        Select True
            Case startwith(name,"A")
                Log($"0: ${name}"$)
            Case startwith(name,"B")
                Log($"1: ${name}"$)
            Case startwith(name,"C")
                Log($"2: ${name}"$)
            Case startwith(name,"D")
                Log($"3: ${name}"$)
            Case Else
                Log("not found")
        End Select
    Next
End Sub

Sub startwith(name As String, c As Char) As Boolean
    Return name.ToUpperCase.StartsWith(c)
End Sub

logs:
I apologize as the actual question is post #3. ??
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Let's face it: the solution found is the most normal thing in this world. I thought the O.P. tried it immediately and it didn't work due to:

I didn't expect Case to support an expression as Java Switch only accepts values
I thought: "He will certainly have tried and evidently in B4X in Select you can only compare values".

Since this is not the case, the problem is a "false problem", a non-existent problem.
 
Upvote 0
Top