iif()

Theera

Well-Known Member
Licensed User
Longtime User
where is iif() function?: BangHead
 

silverbyte

Member
Licensed User
Longtime User
There is no iif function.

You can add it yourself:
B4X:
Sub IIF(c As boolean, TrueRes As String, FalseRes As String)
 If c Then Return TrueRes Else Return FalseRes
End Sub

...
Log(IIF (2 > 3, "yes", "no"))

Not the best solution. Lets say I want to return a boolean, array, object etc etc.. Since b4a doesn't support overloading methods YET, IIF isn't really practical I would need IIF for every returntype. Unless theres a solution to that? casting variables??

Another consideration is the following (in C language)

(1 = 2) ? true : false ==> returns false

Yes I know this is the same as IF ELSE although im writing some code that needs TONS of these conditions and write ifelse everywhere will just make it look yucky.
 

agraham

Expert
Licensed User
Longtime User
It should work using Object parameters as long as you assign the return to the correct type of variable or one compatible for which Basic4android can do the automatic conversion. e.g a String numeric to an Int.

B4X:
Sub IIF(c As Boolean, TrueRes As Object, FalseRes As Object) As Object
 If c Then Return TrueRes Else Return FalseRes
End Sub
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Perhaps I should emphasise that by using Object as the parameter and return type you should be able to use just any reference type as well primitives, arrays, custom types, Views etc. Basic4android will generally convert primitive values to other primitive values but reference types need the return assigned to a compatible type.
 

silverbyte

Member
Licensed User
Longtime User
It should work using Object parameters as long as you assign the return to the correct type of variable or one compatible for which Basic4android can do the automatic conversion. e.g a String numeric to an Int.

B4X:
Sub IIF(c As Boolean, TrueRes As Object, FalseRes As Object) As Object
 If c Then Return TrueRes Else Return FalseRes
End Sub

Thanks agraham! i'll give that a try
 
Top