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.
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
I use this in my String Functions module. It is dimmed as strings and works with numbers, strings or booleans. I have used it this way for more than a year and in many apps with no problem. B4A is very smart handling this!!
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.
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