B4A Library [B4X][B4XLib] B4XExecCondLM

Yesterday I was programming (sometimes it happens to me) and I realized that I... don't know now if I need this kind of feature, so I developed it 😄

Seriously, I seem to remember something like the commands I wrote in this library and which could therefore be useful.

Don't forget that this developer needs coffee in order for him to post decent stuff :)

V. 1.10 - Added ExecIfAll - see post #9
1631460955759.png



ExecIf
1631274932195.png


ExecOnIndex
1631275049340.png


Example:
B4X:
Private Sub Button1_Click
    'ExecIF example.
    Dim A As Int = 10
    Dim B As Int = 2

    Dim Result As Int
    Result = B4XExecCondLM.ExecIF(Array As Boolean(a <= b, a > b), Array As String("Multiply", "Divide"), Array As Object(10, 2), True, Me)
    Log("ExecIf result: " & Result)
 
    'ExecOnIndex example.
    B4XExecCondLM.ExecOnIndex(1, Array As String("Zero", "One", "Two"), Null, False, Me)
End Sub


Public Sub Multiply(Value1 As Int, Value2 As Int) As Int
    Return Value1 * Value2
End Sub

Public Sub Divide(Value1 As Int, Value2 As Int) As Double
    Return Value1 / Value2
End Sub


Public Sub Zero
    Log("Zero")
End Sub

Public Sub One
    Log("One")
End Sub

Public Sub Two
    Log("Two")
End Sub
Log:
ExecIf result: 5
One

Library and test project attached.
 

Attachments

  • B4XExecContLMTest.zip
    14.3 KB · Views: 194
  • B4XExecCondLM.b4xlib
    1.2 KB · Views: 161
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Very loosely, ExecIf is a bit like IIF.
The difference is that IIF returns a value based on a single condition, ExecIf runs a routine selected from N routines (which can be functions) based on N conditions.
 

LucaMs

Expert
Licensed User
Longtime User
I don't know on what occasions ExecIf can be useful but it could, so it is better to have it than not to have it.

While ExecIf runs the first routine whose corresponding condition is True, the ExecIfAll I added right now executes ALL the routines whose conditions are True (obviously consecutively, not simultaneously).

Very theoretical example. You have a byte:
1010 0011
you want to run a (different or the same) routine for each 1.

Updated B4XExecCondLM to V. 1.10.
 
Last edited:
Top