[Bug] assign null to string

Eduard

Active Member
Licensed User
Longtime User
B4X:
   Dim x As String
   x = Null
   If x = Null Then 
      Msgbox("x=null","")
   Else
      Msgbox("x<>null","")
   End If
   If x="" Then 
      Msgbox("weird","")
   End If
   If x="null" Then 
      Msgbox("o no!","")
   End If

The expected behavior would be that x = null would evaluate to true since this is the way in B4A to check for null values.

However. x = "null" evaluates to true! I believe this is a bug.
 

Vader

Well-Known Member
Licensed User
Longtime User
In a different language I would suggest that a string is not nullable.

IE, because you created a string, it cannot be null, rather it would be an empty string.

That's my guess.
 

melamoud

Active Member
Licensed User
Longtime User
Its not a bug its how it works in b4a
String can not be null so it translate the null to "null"

If you want thus ability make the type object and translate / cast to string when you need it as string ...
 

francoisg

Active Member
Licensed User
Longtime User
If you want a simple way to check for any kind of object (strings as well as other classes) you can use the following functions:

Sub isNull (o As Object) As Boolean
Return ((o = Null) Or (o = "null"))​
End Sub

Sub ifNull (o As Object, default As Object) As Object
If isNull(o) Then
Return default​
Else
Return o​
End If​
End Sub
 
Last edited:

rboeck

Well-Known Member
Licensed User
Longtime User
If you want a simple way to check for any kind of object (strings as well as other classes) you can use the following functions:

Sub isNull (o As Object) As Boolean
Return ((o = Null) Or (o = "null")) '<------------​
End Sub

Hi, i was looking long time for this type of function; now i tried it, but i get an error in the second row: 'wrong input format' (translated from german); any idea, whats wrong?
 

francoisg

Active Member
Licensed User
Longtime User
Hi,
I think the code is formatted wrong (remove all spaces in front of the lines, replace with tabs and try again). I had to play with formatting the code in order to display correctly on the web site.
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

that helped; i had to type it myself and the mistakes went away. Strange...
Thanks for your help!
 
Top