Just think of it as a helper function, as a quality-of-life thing for the developers. If you don't like it, I'm certain the old way will continue to work.
Its implementation is actually a bit more complicated than it might seem and even required an update the compiler itself.It's a new internal function
2 - WHYYYYYYY?
Sub IsNull (x As Object) As Boolean
Return x = Null
End Sub
B4X:Sub IsNull (x As Object) As Boolean Return x = Null End Sub
"" is a zero-length string, not a Null objectbut is it always working ...?
for example strings = ""
yeap it is... but some.... are saying it is also "null""" is a zero-length string, not a Null object
'return "", if no data
Sub GetStringFromSQL (SQL As SQL, req As String) As String
Dim a As String
Try
a = SQL.ExecQuerySingleResult(req)
Catch
Return ""
End Try
If a = Null Then
Return ""
Else If a.ToLowerCase = "null" Then
Return ""
Else If a = "" Then
Return ""
Else
Return a
End If
End Sub
Sub GetBooleanFromSQL (SQL As SQL, req As String) As Boolean
Dim a As String
Try
a = SQL.ExecQuerySingleResult(req)
Catch
Return False
End Try
If a = Null Then
Return False
Else If a.ToLowerCase = "null" Then
Return False
Else If a = "" Then
Return False
Else if a = "0" Or a.ToLowerCase = "false" Then
Return False
Else
Return True
End If
End Sub
'return -2147483648, if no valid number data
Sub GetIntFromSQL (SQL As SQL, table As String, req As String) As Int
Dim Const NegativeRes As Int = -2147483648
Dim q As Int = SQL.ExecQuerySingleResult("SELECT count(*) FROM " & table)
If q = 0 Then
Return NegativeRes
End If
Dim a As String
Try
'req may be wrong syntax, dangerous
a = SQL.ExecQuerySingleResult(req)
Catch
Log("GetIntFromSQL(" & req & ").error = " & LastException)
Return NegativeRes
End Try
If a = Null Then
Return NegativeRes
Else If a.ToLowerCase = "null" Then
Return NegativeRes
Else If IsNumber(a) = False Then
Return NegativeRes
Else
Dim res As Int = a
Return res
End If
End Sub
'return -9223372036854775808, if no valid number data
Sub GetLongFromSQL (SQL As SQL, table As String, req As String) As Long
Dim Const NegativeRes As Long = -9223372036854775808
If TableExists(SQL, table) = False Then
Log("GetIntFromSQL(" & table & ") does not exist")
Return NegativeRes
End If
Dim q As Int = SQL.ExecQuerySingleResult("SELECT count(*) FROM " & table)
If q = 0 Then
Return NegativeRes
End If
Dim a As String
Try
'req may be wrong syntax, dangerous
a = SQL.ExecQuerySingleResult(req)
Catch
Log("GetIntFromSQL(" & req & ").error = " & LastException)
Return NegativeRes
End Try
If a = Null Then
Return NegativeRes
Else If a.ToLowerCase = "null" Then
Return NegativeRes
Else If IsNumber(a) = False Then
Return NegativeRes
Else
Dim res As Long = a
Return res
End If
End Sub
well, except the case for String.but is it always working ...?
In case of SQL, I will use IFNULL() or isNull() and return a default value.Is it bad practice ?
Erel how will this affect custom Java libraries? Will it still call XYZ.IsInitialized under the hood? Or will we need to update our libraries to support this.B4X:'old If Lst = Null Or Lst.IsInitialized = False Then Return 'new If IsInitialized(Lst) = False Then Return
Erel how will this affect custom Java libraries? Will it still call XYZ.IsInitialized under the hood? Or will we need to update our libraries to support this.
And the old ways will of course continue to work.
I expect it will be incorporated into the Core libraries on next version update for each platform (B4A, B4i, B4J).Yes thanks I already saw that but that doesn’t answer the question.
Yes the old ways are still supported but if we want to support IsInitialized(XYZ) is that a code change or is it handled transparently?
The most common case that requires our attention is an object that is NOT initialized (like your example shows). I would like to ask for a keyword IsNotInitalized() to make it even more easy. Thanks!B4X:'old If Lst = Null Or Lst.IsInitialized = False Then Return 'new If IsInitialized(Lst) = False Then Return
Erel mentioned this new keyword required the update of the compilation process. So, i think it's already implemented and tested. And will not be inverted already.ask for a keyword IsNotInitalized()
If Not(IsInitialized(Lst)) Then Return