Android Question Value null in float or Integer

rossati

Active Member
Licensed User
Longtime User
Hello

why I can assign null value to a float (or Integer) variable and I can't test it?
B4X:
    Dim integer As Int = Null ' this works
    If integer = Null Then ... ' this crashes
Regards
Giovanni Rossati
 

MarkusR

Well-Known Member
Licensed User
Longtime User
"int" is not an object/class , so i guess it cast into value 0.
me would also expect the same behavior.
integer did not have null.
the compiler should catch this.

 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi rossati,

I did some experimenting and discovered you can't assign Null to any numeric variables but you can do it to strings. For numeric variables you can only assign a number to them such as 0 or some other number. The "If" statement never got a chance to execute. I commented out the coding that will make your app crash.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim strMyString As String = Null

    If strMyString = Null Then
        ' This works.
    End If

'  Dim lngMyLong as Long = Null

'    Dim integer As Int = Null ' this works
    
'    If integer = Null Then
'          ' this crashes
'    End If
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You can assign null to variables that work by reference, all numeric variables or basic variables work by value and not by reference

Objects, classes, or non-basic variables work as references and can be assigned with null
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
yes, this basic types are exceptional case in OOP ^^
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…