Android Question How to assign NULL value to variable

xuwb

Member
Licensed User
Longtime User
Hi!
I have this code:
----------------------
Dim btn As Button
btn = Null
If btn = Null Then
Msgbox("btn is null", "")
Else
Msgbox("btn isn't null", "")
End If
----------------------

I hope to show "btn is null", but it shows "btn isn't null" really. What's wrong with this code?
 

DonManfred

Expert
Licensed User
Longtime User
With btn = Null you would discard the btn you have DIMmed. You cannot do that i think.
BUT you can use the Tag of your button

B4X:
    Dim btn As Button
    btn.Initialize("btn")
    btn.Tag = Null
    If btn.Tag = Null Then
    Msgbox("btn is null", "")
    Else
    Msgbox("btn isn't null", "")
    End If

That shows "btn is null" as expected

Maybe you show more code or explain more detailed what you want to realize with your btn = null. This could help to give help to you
 
Upvote 0

xuwb

Member
Licensed User
Longtime User
I have a Sub to create a Button. In Sub, create a Button according to a condition. If the condition is not met, returns a NULL. The code just like this:
----------------------------
Sub MyCreateButton(condition As String) As Button
Dim btn As Button
btn.Initialize("btn")
Select condition
Case "1"
btn.Text = "Some text 1"
btn.Color = Colors.Red
Case "2"
btn.Text = "Some text 2"
btn.Color = Colors.Yellow
Case Else
'Other value is not expected, so we return a NULL
btn = Null
End Select
Return btn
End Sub
----------------------------

Now I call this Sub like this:
----------------------------
Dim btn As Button
btn = MyCreateButton(EditText1.Text)
If btn = Null then
Msgbox("Bad value, Input again!", "")
Else
....... some code for using btn, omitted.
End If
----------------------------

The problem is that "If btn = Null" will be always False whether I input any value.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please use [ CODE]your code here[ /CODE] (without the spaces) when you post code...

Dont use

B4X:
btn = Null
End Select
Return btn

do a

B4X:
Return null

in case there´s something wrong...

OR even better it would be that you DONT DIM the button inside your sub and return a null if there is something wrong....

For example:

B4X:
Sub MyCreateButton(condition As String) As Button
  Select condition
  Case "1"
    Dim btn As Button
    btn.Initialize("btn")
    btn.Text = "Some text 1"
    btn.Color = Colors.Red
    return btn
  Case "2"
    Dim btn As Button
    btn.Initialize("btn")
    btn.Text = "Some text 2"
    btn.Color = Colors.Yellow
    return btn
  Case Else
  'Other value is not expected, so we return a NULL
    return null
  End Select
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
In addition to my last answers i have edited a smal example-app.

There are 5 dynamiccaly added buttons. AND the have an click-sub which are passed to MyCreateButton as second parameter...
 

Attachments

  • buttontest.zip
    6.3 KB · Views: 176
  • main.png
    main.png
    19.1 KB · Views: 225
  • buttonclicked.png
    buttonclicked.png
    19.7 KB · Views: 215
Upvote 0
Top