B4J Question [solved] Test Null?

Discussion in 'B4J Questions' started by MarkusR, Jul 17, 2019.

  1. MarkusR

    MarkusR Well-Known Member Licensed User

    its a bug?
    output is
    null
    null
    Code:
    'B4J 7.51
    Sub Test2
        
        
    Dim Params As Map
        Params.Initialize
        
    'Params.Put("Key","ABC") 'without this row .Get Return something that can Not be Test As null
        
        
    Dim MyString As String
        MyString = Params.Get(
    "Key")
        
    Log(MyString)
        
    'If IsNull(MyString) Then MyString = "123"
        If MyString = Null Or MyString = "" Then MyString = "123"
        
    Log(MyString)
        
    End Sub

    Sub IsNull(O As Object) As Boolean
        
    Return (O=Null)
    End Sub
     
  2. amykonio

    amykonio Member Licensed User

    Hi.
    What happens if you change:
    Code:
    MyString = Null
    to
    Code:
    MyString = "null"

    Does it work?

    Anyway. The way I would write it is
    Code:
    Sub Test2
     
    Dim Params As Map
     
    Dim o As Object
     Params.Initialize
    ' Params.Put("Key","ABC") 'without this row .Get Return something that can Not be Test As null
     
     
    Dim MyString As String
     o = Params.Get(
    "Key")
     
    If o Is Object Then
      MyString = o
     
    Else
      MyString = 
    "123"
     
    End If
     
    Log(MyString)
    End Sub
     
    MarkusR likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    The Null value is converted to "null" string.

    The less you work with nulls the better, especially with strings. It is better to write it like this:
    Code:
    Dim MyString As String = Params.GetDefault("Key""")
    If MyString = "" Then ...
     
    Jmu5667 and MarkusR like this.
  4. MarkusR

    MarkusR Well-Known Member Licensed User

    hi erel, ahhh :)
    that Null is converted to "null" string is special.
    because missing quotes maybe you can modify this window that is it more clear what kind of value is displayed.
    MyString null String
    MyString null Object
    or
    MyString = "null" to be equal to source code


    Snap_2019.07.18_09h20m10s_002_.png
     
  5. amykonio

    amykonio Member Licensed User

    I'm not sure this would be helpful...
    All values for strings in this window are displayed without quotes. Also, how can we know if the value "null" wasn't set as a string?
    This is the main reason I believe Erel suggests to avoid working with nulls - strings...
     
    Erel likes this.
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    intuitiv i would expect empty string "" if there is no key found. and a method Params.KeyExists("Key")
    other languages would raise an error but i not like error handling for every method.
    Code:
    Dim MyString As String
    MyString = Params.Get(
    "Key")
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    There is one of course:
    Code:
    If Map.ContainsKey("Key")
    It is problematic in this case to return empty string in the case of null (and it might break existing programs).
     
    MarkusR likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice