Android Question if syntax with variable

DALB

Active Member
Licensed User
Hello,
is it possible to write something close to this syntax ?

B4X:
Starter.vPays=If (valPays.Text=Null Then valPays.Text="")

Like I used in an other language (VBA : variable = iif (this boolean then that else thisOther)

Thank you.
 

DonManfred

Expert
Licensed User
Longtime User
somethink like this maybe?

B4X:
Sub IIf(Condition As Boolean, Yes As Object, No As Object) As Object
   If Condition Then Return Yes Else Return No
End Sub

'
'Usage
Starter.vPays=IfF((valPays.Text=Null),"",valPays.Text)
 
Upvote 0

emexes

Expert
Licensed User
+1 Manfred's solution, but... the example from post #1
B4X:
Starter.vPays=If (valPays.Text=Null Then valPays.Text="")
which doesn't have an Else branch, looks like it is just meant to do this:
B4X:
If valPays.Text = Null Then valPays.Text = ""
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
For the example you gave, given that a program that needs it is likely to use it a lot, it might be better to write a type-specific function eg:
B4X:
Sub NullToString(S As String) As String

    If S = Null Then
        Return ""
    End If

    Return S

End Sub

'Usage (and readability comparison)

Starter.vPays = NullToString(Starter.vPays)               'type-specific function

Starter.vPays=IfF((valPays.Text=Null),"",valPays.Text)    'general function
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Starter.vPays=If (valPays.Text=Null Then valPays.Text="")

I spent a couple of minutes trying to parse this and I can't even begin to understand what's supposed to happen. Can you walk me through it like I'm five?


Note: I've seen DonManfreds suggestion, and tried to use that as a guide to undersanding the example by DALB, but I can't see how they relate really. I mean, DonManfred is basically creating a simple ternary operator, but I can't see how that relates to the original question.

I'm probably missing something obvious here.
 
Upvote 0

DALB

Active Member
Licensed User
ok yes, there is not a native function, I must create a sub for this.
I'm a really lazy man and copy ans paste is my favorite action.
But here, sub as DonManfred shows the example creates 3 lines, and I must add my own one...total = 4 lines.

Yes, it's necessary to create my own solution (sub)
Thanks to everyone.
 
Upvote 0

emexes

Expert
Licensed User
I'm probably missing something obvious here.
To be fair to DALB, and especially given that he specified "something close", the pseudocode seemed pretty clear to me. And Manfred* too. Perhaps the C-style assignment-within-expressions and ternary operators don't jump off the page to hardcore dedicated BASIC programmers.

There is a missing closing bracket around the VBA reference, but we've all done that at least once ;-)


* the King of Precision Literal Interpretation - but I do appreciate the accuracy that he adds to my hand-wavy approximations :)
 
Upvote 0
Top