Android Question Case question

anaylor01

Well-Known Member
Licensed User
Longtime User
The below case statement isn't working. Anyone know why?
B4X:
Dim vBonus As String
            Select  vBonus
               Case  PD = "Points" And TmSc > PvSc + 50  And TmSc < PvSc +  80
                   Msgbox("20 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 70  And TmSc < PvSc +  100
                   Msgbox("40 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 90  And TmSc < PvSc +  120
                   Msgbox("60 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 110  And TmSc < PvSc +  140
                   Msgbox("80 Point bonus","Bonus")

                Case PD = "Drinks" And TmSc > PvSc + 5 And TmSc < 8
                    Msgbox("2 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 7 And TmSc < 10
                    Msgbox("4 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 9 And TmSc < 12
                    Msgbox("6 Point Bonus","Bonus")
                Case PD = "Drinks" And  TmSc > PvSc + 11 And TmSc < 14
                    Msgbox("8 Point Bonus","Bonus")
            End Select
 

Descartex

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
If  PD = "Points" And TmSc > PvSc + 50  And TmSc < PvSc +  80 Then
    Msgbox("20 Point bonus","Bonus")
Else If PD = "Points" And TmSc > PvSc + 70  And TmSc < PvSc +  100 Then
        Msgbox("40 Point bonus","Bonus")
   Else If  PD = "Points" And TmSc > PvSc + 90  And TmSc < PvSc +  120 Then
            Msgbox("60 Point bonus","Bonus")
        Else If  PD = "Points" And TmSc > PvSc + 110  And TmSc < PvSc +  140 Then
                Msgbox("80 Point bonus","Bonus")
            Else If PD = "Drinks" And TmSc > PvSc + 5 And TmSc < 8 Then
                    Msgbox("2 Point Bonus","Bonus")
                Else If PD = "Drinks" And TmSc > PvSc + 7 And TmSc < 10 Then
                    Msgbox("4 Point Bonus","Bonus")
                    Else If     PD = "Drinks" And TmSc > PvSc + 9 And TmSc < 12 Then
                            Msgbox("6 Point Bonus","Bonus")
                        Else If PD = "Drinks" And  TmSc > PvSc + 11 And TmSc < 14 Then
                                Msgbox("8 Point Bonus","Bonus")
                            End If
                        End If  
                    End If
                End If
            End If
        End If
    End If
End If

You cant to put more than a parameter to Select Conditioning.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use:
B4X:
Select True
                Case  PD = "Points" And TmSc > PvSc + 50  And TmSc < PvSc +  80
                   Msgbox("20 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 70  And TmSc < PvSc +  100
                   Msgbox("40 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 90  And TmSc < PvSc +  120
                   Msgbox("60 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 110  And TmSc < PvSc +  140
                   Msgbox("80 Point bonus","Bonus")

                Case PD = "Drinks" And TmSc > PvSc + 5 And TmSc < 8
                    Msgbox("2 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 7 And TmSc < 10
                    Msgbox("4 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 9 And TmSc < 12
                    Msgbox("6 Point Bonus","Bonus")
                Case PD = "Drinks" And  TmSc > PvSc + 11 And TmSc < 14
                    Msgbox("8 Point Bonus","Bonus")
            End Select
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The below case statement isn't working. Anyone know why?
B4X:
Dim vBonus As String
            Select  vBonus
               Case  PD = "Points" And TmSc > PvSc + 50  And TmSc < PvSc +  80
                   Msgbox("20 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 70  And TmSc < PvSc +  100
                   Msgbox("40 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 90  And TmSc < PvSc +  120
                   Msgbox("60 Point bonus","Bonus")
               Case  PD = "Points" And TmSc > PvSc + 110  And TmSc < PvSc +  140
                   Msgbox("80 Point bonus","Bonus")

                Case PD = "Drinks" And TmSc > PvSc + 5 And TmSc < 8
                    Msgbox("2 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 7 And TmSc < 10
                    Msgbox("4 Point Bonus","Bonus")
                Case PD = "Drinks" And TmSc > PvSc + 9 And TmSc < 12
                    Msgbox("6 Point Bonus","Bonus")
                Case PD = "Drinks" And  TmSc > PvSc + 11 And TmSc < 14
                    Msgbox("8 Point Bonus","Bonus")
            End Select

Because you used vBonus then you should "list" possible values of it. This is how Select Case should work.

I mean:

B4X:
Select Case vBonus
    Case vBonusValue1  [or complex condition but related to vBonus]
       Do1
    Case vBonusValue2
...

Given that you need to check other variables, instead, you should use the Erel's code.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Above suggestions will fix your problem, however and just for fun, I wonder whether the following works too :)
B4X:
Dim points As Int=(Abs(Ceil((tmsc-pvsc-50)/30))+Ceil((tmsc-pvsc-50)/30))*10*(pd.IndexOf("Points")+1)+(Abs(Ceil((tmsc-pvsc-5)/3))+Ceil((tmsc-pvsc-5)/3))*(pd.IndexOf("Drinks")+1)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
I have too much time in my hands.... :rolleyes:

B4X:
Dim interval   = 30         As Int
Dim minValue   = 50         As Int
Dim ptsValue   = 20         As Int
Dim points     = 0          As Int
Dim isPoints   = False      As Boolean
Dim isDrinks   = False      As Boolean
Dim outputStr  = ""         As String
Dim msgTitle   = "Bonus"    As String

For i = 1 To iterations
    outputStr = ""

    isPoints = ( _
        (PD == "Points") _
        And _
        (TmSc > PvSc + minValue + points) _
        And _
        (TmSc < PvSc + minValue + points + interval) _
    )

    isDrinks = ( _
        (PD == "Drinks") _
        And _
        (TmSc > PvSc + (minValue + points) * 0.1) _
        And _
        (TmSc < PvSc + (minValue + points + interval) * 0.1) _
    )

    points = points + ptsValue

    If isPoints Then
        outputStr = points & "Point bonus"
    Else _
    If isDrinks Then
        outputStr = (points * 0.1) & "Point bonus"
    End if
    If outputStr <> "" Then MsgBox(outputStr, msgTitle)
Next
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Above suggestions will fix your problem, however and just for fun, I wonder whether the following works too :)
B4X:
Dim points As Int=(Abs(Ceil((tmsc-pvsc-50)/30))+Ceil((tmsc-pvsc-50)/30))*10*(pd.IndexOf("Points")+1)+(Abs(Ceil((tmsc-pvsc-5)/3))+Ceil((tmsc-pvsc-5)/3))*(pd.IndexOf("Drinks")+1)

I don't know if it can work, but...

readability!

I don't like also variable names like PD, TmSc and PvSc - same reason
 
Upvote 0
Top