B4J Question [Solved] Sub receive different classes type without use Object in its signature

Xandoca

Active Member
Licensed User
Longtime User
Hi,

I want to implement a sub that can receive many types of classes as parameter (e.g. CustomLabel, CustomTextField etc.). Today I'm doing the following:
B4X:
Public Sub AddElement(element As Object)
    If element Is CustomLabel Then
        ...
    else if element Is CustomTextField then
        ...
    ...
End Sub

I would like to implement something like:
B4X:
Public Sub AddElement(element As CustomComponent)
    If element Is CustomLabel Then
        ...
    else if element Is CustomTextField then
        ...
    ...
End Sub

"element As CustomComponent" instead of "element as Object".

How can this be accomplished?

And... What is the best way to handle the component type inside the sub? (is "If element Is CustomLabel Then" a good solution ?)

Thank you very much.
 

EnriqueGonzalez

Expert
Licensed User
Longtime User
It may be better if all of them are UI elements then use Node. You could also use B4xView.

If they are different things then it is not possible without creating your own Java class and inheriting Object or other low level class
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Thank you Enrique.

They are not UI elements, it was just for explain.

They are not Java Class. They are B4J classes.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If these are actual views then you should use B4XView. If these are custom views then pass View.mBase and add it.

General solution with classes:
B4X:
Public Sub AddElement(Element As Object)
 CallSub(Element, "DoSomething')
End Sub

You should have a 'DoSomething' sub in each of the classes that does whatever needed.
 
Upvote 0
Top