Bug? Colon (:) with single line IF statement causes a problem later with nested IF

MrKim

Well-Known Member
Licensed User
Longtime User
I posted a screenshot rather than the code so you could see the error.
Putting the IF statements highlighted in yellow on their own lines solved the problem.
Only ONE if statement on the same line with a colon is required to cause the error.
Note that the failure is in the NESTED IF, not the outer IF.

The reason this was hard to debug is the nested if was added (I think) while working on the B4A version so I didn't see a problem until I came back to B4J which is where the problem code is used..
The
B4X:
'catch' expected
log error also threw me off.
I remmed the #If and found the error was in both B4A and B4J
(Note that CallBack is just a variable name.)
1680425163699.png

Here is the Log Error:
1680427377673.png


Yes, if I was doing this today I would use regex to split the file. This is a B4X mod of code that has been working for 25 years. The B4X mod itself is over 5 years old. Why reinvent the wheel and confuse my partner?

I like using the : to keep code on the same line when I have short, repeating, similar blocks of code. Let's me see more on the screen - but screen resolutions are a lot better today than they were 40 years ago and monitors are a lot bigger. It wouldn't break my heart if you removed that feature.

I am grateful that a compile error was thrown rather than some weirdness when the code runs. As always you do a fantastic job! Love working with B4X.
 

MrKim

Well-Known Member
Licensed User
Longtime User
On a quick try I am unable to replicate this. Can you post the actual code block.
There are a lot of dependencies, but sure:
B4X:
Public Sub Connect(CallBack As String) As ResumableSub
    
'    Note that you can use ConnectionPool from jServer library To Connect To remote databases. It will take care of managing the connections.
    Try
        SKSQL.Close
        'File.OpenInput(File.DirApp, "sk2000.pth")
        Dim ConnStr As String = "", Srvr As String, UID As String, PWD As String
        #If B4J
            Dim X As Int
            Try
                ConnStr = File.ReadString(File.GetFileParent(File.GetFileParent(File.DirApp)), "sk2000.pth")
            Catch
                Try
                    ConnStr = File.ReadString(File.GetFileParent(File.DirApp), "sk2000.pth")
                Catch
                    MP.SQLDirect = False
                    Return 1
                End Try
            End Try
    '        If (DADMod.IsNull(T)) Or (T.Length = 0) Then
    '            MP.Msgbox($"Cannot find the file sk2000.pth in ${File.GetFileParent(File.GetFileParent(File.DirApp))} or File.ReadString(File.GetFileParent(File.DirApp) make sure this file is in the appropriate directory and this program has the rights to READ the file!"$, "File Not Found")
    '            Wait For Msgbox_Result (Result As Int)
    '        End If


            ConnStr = ConnStr.SubString2(0, ConnStr.IndexOf(CRLF))
            Srvr = ConnStr.SubString(ConnStr.IndexOf("SERVER=") + 7)
            X = Srvr.IndexOf(";"): If X < 0 Then X = Srvr.Length
            Srvr = Srvr.Replace(",", ":")   '9/9/22
            Srvr = "//" & Srvr.SubString2(0, X)
            DBName = ConnStr.SubString(ConnStr.IndexOf("DATABASE=") + 9)
            X = DBName.IndexOf(";"): If X < 0 Then X = DBName.Length
            DBName = DBName.SubString2(0, X).As(String).Trim
            UID = ConnStr.SubString(ConnStr.IndexOf("UID=") + 4)
            X = UID.IndexOf(";"): If X < 0 Then X = UID.Length
            UID = UID.SubString2(0, X)
            PWD = ConnStr.SubString(ConnStr.IndexOf("PWD=") + 4)
            X = PWD.IndexOf(";"):If X < 0 Then X = PWD.Length
            PWD = PWD.SubString2(0, X)



    '        MP.Msgbox(Srvr & CRLF & DB & CRLF & UID & CRLF & PWD , "World")
    '        Wait For Msgbox_Result (Result As Int)
            'MICROSOFT DRIVER WORKING! USING SERVER NAME!
            
            'SKSQL.InitializeAsync("Connect", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://KIMSASUS\KIMSASUSSKSQL4:65218;databaseName=skdataDemo;user=skdata;password=shopkeeper;", "skdata", "shopkeeper")
            SKSQL.InitializeAsync("Connect", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver:" & Srvr & ";databaseName=" & DBName & ";user=" & UID & ";password=" & PWD & ";", UID, PWD)
        #Else
            'SKSQL.in("Connect", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver:" & Srvr & ";databaseName=" & DBName & ";user=" & UID & ";password=" & PWD & ";", UID, PWD)
            SKSQL.InitializeAsync("Connect", Main.driver, Main.jdbcUrl, Main.Username, Main.Password)
            #If Debug
                Log($"SKSQL.InitializeAsync(Connect, ${Main.driver}, ${Main.jdbcUrl}, ${Main.Username}, ${Main.Password})"$)
            #End If
        #End IF
        Wait For Connect_Ready (Success As Boolean)
        If CallBack.EqualsIgnoreCase("ConnTest") Then
            If Success = False Then
                Return LastException.Message
            Else
                Return "Success"
            End If
        Else
            If Success = False Then
                Return -1
                Sleep(5000)  'wait for form to open before displaying message
'                MP.xui.MsgboxAsync("PTH file was found but Failed connect to the server. Server is not available, database is not available, connect string is wrong, or you are having network/firewall issues. Contact your system administrator.", "Network/SQL Error")
'                MP.xui.MsgboxAsync(LastException.Message.SubString2(0, Min(LastException.Message.Length, 500)), "Java Error:")
            Else
                MP.SQLDirect = True
                #If Debug
                #If not(B4J)
                MP.Toast.VerticalCenterPercentage = 10
                    MP.Toast.Show("Using Direct SQL Connection", Null)
                #End If
                #End If
            End If
            Return 2
        End If
    Catch
        If ConnStr.Length > 0 Then
            ConnStr = $"${ConnStr}
connection string was parsed as:
SERVER=:    ${Srvr}
DATABASE=:    ${DBName}
UID=:        ${UID}
PWD=:        ${PWD}"$
        End If
        NetFailed(ConnStr)
    End Try
    Return 3
End Sub

Note that is the stuff inside the #If B4j that is causing the problem
 

agraham

Expert
Licensed User
Longtime User
I see it. It only happens if the Sub is declared as a Resumable Sub and, as you say, there is a nested If statement.
B4X:
Sub testit As ResumableSub
    Dim x As Int = 23 : If x = 2 Then x = 24   
    If x = 23 Then
        If x = 24 Then
            x = 26
        End If
    End If       
End Sub
 

MrKim

Well-Known Member
Licensed User
Longtime User
I see it. It only happens if the Sub is declared as a Resumable Sub and, as you say, there is a nested If statement.
Pretty obscure. I never would have thought about it being related to resumable subs.
 
Top