B4A Library [B4X] B4XIndexOfObject

Authors: @Sagenut & @LucaMs


Multi-platform library (B4A, B4J, B4i) that provides only one very useful method: Find.

Its purpose is to be able to obtain the index of a List like the IndexOf method easily even if the elements contained in the List are objects and not simple strings or numbers.

Furthermore, it also works with Maps, returning the key relating to the object contained in them and searched for.

[See post #2]

[For example, it would have been useful in this case].


B4X Library and example project attached.
 

Attachments

  • B4XIndexOfObject.b4xlib
    1.1 KB · Views: 132
  • IndexOfObject_Example.zip
    9.5 KB · Views: 132
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I'll try to explain the problem better (even if it's night (3 AM) and I'm watching TV, "Law & order" ?).

If you have an objOne object, add it to a List, then you can write:
B4X:
If MyList.IndexOf(objOne) <> - 1 Then
' found
End If

But if you create a new object objNew, whose contents (properties) are the same as objOne and then look for:
B4X:
If MyList.IndexOf(objNew) <> - 1 Then
' found
End If

it will not be found, as they are two distinct objects.
Sometimes, however, you want to perform the search exactly in the way indicated above, i.e. based on the contents of the object, to check whether an object of the same type already exists in the List and which has the same content.

Furthermore, B4XIndexOfObject.Find also works with Maps, while there is no MyMap.IndexOf method.

I hope I have been exhaustive; if not, let other members explain it ?


P.S. Well, now a simpler explanation has come to mind: it is very useful for searching in a List or Map for an object that is identical to another (not the same).
 
Last edited:
Top