Select Case Problem

Scantech

Well-Known Member
Licensed User
Longtime User
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim Timer_Test As Timer
   Dim DataType As String
   Dim NumberType As Int
   Dim ColorType As Int
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Timer_Test.Initialize("Timer_Test", 1000)

End Sub

Sub Activity_Resume
   Timer_Test.Enabled = True
   DataType = "Test1"
   NumberType = 0
   ColorType = 1
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event

End Sub
Sub Activity_Pause (UserClosed As Boolean)
   Timer_Test.Enabled = False
End Sub
Sub Timer_Test_Tick
   Select Case DataType
      Case "Test1"
         ToastMessageShow("Test1", False)
         Select Case NumberType 
            Case 0:   ToastMessageShow("NumberType 0", False)
            Case 1:   ToastMessageShow("NumberType 1", False)
         End Select
         
         Select Case ColorType 
            Case 0:   ToastMessageShow("ColorType 0", False)
            Case 1:   ToastMessageShow("ColorType 1", False): DataType = "Test2"
         End Select
         
      Case "Test2"
         ToastMessageShow("Test2", False)
   End Select
End Sub

I don't know what the deal is with Select case. How many are you allowed to have inside a select case? That above code will not work properly. ToastMessageShow("Test2", False) will not get raised. B4A does not detect this error. Does Java allow this kind of coding?
 
Last edited:

Scantech

Well-Known Member
Licensed User
Longtime User
Hello Klaus,

Thank you for your quick reply. That did not solve it. I have always used Select Case on many of my apps without any problems. The problem only occurs in the example above. I will video capture when I get the chance to show what I am talking about.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If I'm understanding your code correctly, you are trying to change the variable that has been used in the select case, after the select has started. It seems logical to me that this won't work.

The decision as to which case is valid has already been made before you change the variable.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I wasn't reading you code correctly, adding a log(DataType) to the Timer_Test_Tick shows that the variable DataType does get changed on the first run through.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
If I'm understanding your code correctly, you are trying to change the variable that has been used in the select case, after the select has started. It seems logical to me that this won't work.

The decision as to which case is valid has already been made before you change the variable.

Hello Steve,

Check out the video and see what I am talking about. If what your saying is correct then maybe Erel can trap this problem during compiling.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi the following code shows an example of nested case that works,
B4X:
   Dim job As String 
   Dim job2 As Int
   job = "two"
   job2 = 2
   
   Select job
      Case "one"
         Log("one")
      Case "two"
         Log("two")
         Select job2
            Case 1
               Log("1")
            Case 2
               Log("2")
         End Select
      Case "three"
         Log("three")
   End Select
also you may want to increase the time a little bit for the timer as the toast messages stay on screen for a bit longer
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is interesting,

B4X:
Sub Timer_Test_Tick
   Log("Tick")
   Log("DT : "&DataType)
   
    Select DataType
        Case "Test1"
         
         Log("T1")
         
            ToastMessageShow("Test1", False)
            Select NumberType 
                Case 0:   ToastMessageShow("NumberType 0", False)
                Case 1:   ToastMessageShow("NumberType 1", False)
            Case Else
            End Select
            
            Select ColorType 
                Case 0: ToastMessageShow("ColorType 0", False)
                Case 1:   ToastMessageShow("ColorType 1", False):   DataType = "Test2"
            Case Else
            End Select
            
        Case "Test2"
         
         Log("T2")
            
         ToastMessageShow("Test2", False)
      Case Else
      
         Log("Fail")
         
    End Select
End Sub

remove either of the nested selects (NumberType or ColorType) and it works as expected.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
I have just been trying your code and I think the problem is having 2 'select....end select' statements within the one 'CASE'. I think you will need to put the 'ColorType' select nested inside of the 'NumberType' select.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
And this works:

B4X:
Sub Timer_Test_Tick
   Log("Tick")
   Log("DT : "&DataType)
   
    Select DataType
           Case "Test2"
         
         Log("T2")
            
         ToastMessageShow("Test2", False)
         
        Case "Test1"
         
         Log("T1")
         
            ToastMessageShow("Test1", False)
            Select NumberType 
                Case 0:   ToastMessageShow("NumberType 0", False)
                Case 1:   ToastMessageShow("NumberType 1", False)
            Case Else
            End Select
            
            Select ColorType 
                Case 0: ToastMessageShow("ColorType 0", False)
                Case 1:   ToastMessageShow("ColorType 1", False):   DataType = "Test2"
            Case Else
            End Select
            

      Case Else
      
         Log("Fail")
         
    End Select
End Sub

Just found this post
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I have just been trying your code and I think the problem is having 2 'select....end select' statements within the one 'CASE'. I think you will need to put the 'ColorType' select nested inside of the 'NumberType' select.

Correct. I bring this attention to Erel so maybe he can prevent it from compiling successfully.
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
And this works:

B4X:
Sub Timer_Test_Tick
   Log("Tick")
   Log("DT : "&DataType)
   
    Select DataType
           Case "Test2"
         
         Log("T2")
            
         ToastMessageShow("Test2", False)
         
        Case "Test1"
         
         Log("T1")
         
            ToastMessageShow("Test1", False)
            Select NumberType 
                Case 0:   ToastMessageShow("NumberType 0", False)
                Case 1:   ToastMessageShow("NumberType 1", False)
            Case Else
            End Select
            
            Select ColorType 
                Case 0: ToastMessageShow("ColorType 0", False)
                Case 1:   ToastMessageShow("ColorType 1", False):   DataType = "Test2"
            Case Else
            End Select
            

      Case Else
      
         Log("Fail")
         
    End Select
End Sub

Just found this post

Interesting. I have not tried it.
 
Upvote 0
Top