Android Question Passing Types to a Sub

DawningTruth

Active Member
Licensed User
How do I pass a Type to a sub.

I want something like this pseudo code:

B4X:
Dim element 1 As Button
Dim element 2 As Label
Dim element 3 As Webview

----

DoSomething(element1, button, changeColor)
DoSomething(element2, label, changeText)
DoSomething(element3, webview, changeURL)

----
Sub DoSomething (subElement As Type, T As Type, Action As String)

'Do Something here

End Sub
 

emexes

Well-Known Member
Licensed User
That's an interesting question (to which I don't have an immediate answer).

My first line of attack would be the serializer routines: presumably the first bytes from those would indicate what the type is.

Another approach is: cast the thing to an Object, and then inside your routine, have a cascade of something like:

B4X:
Sub DoSomething (subElement as Object, Action As String)

    If subElement Is Button Then
        Dim B as Button = subElement    'cast object to its type
        B.Text = Action    'eg
    Else If subElement Is Label Then
        Dim L as Label = subElement
        L.Text = Action
    Else If subElement Is Webview then
        Dim W as Webview = subElement
        W.Request = Action & " " & URL    '???
    Else
        log("Unrecognized object type")
    End if

End Sub
I haven't actually run that code, but I'm sure it'll work third try :)

edit: fixed typo in code (Object s/b subElement) so as to not confuse future generations
 
Last edited:

emexes

Well-Known Member
Licensed User
Plus there are some types that can stand-in for several derived types, eg a View object can be a Button or Label or etc, but I think you still have to cast them down to the derived type to access the methods etc (sorry about the fuzziness; coincidentally, I am on a similar quest in B4J right now, trying to replicate something I've done in B4A)
 

DawningTruth

Active Member
Licensed User
That's an interesting question (to which I don't have an immediate answer).

My first line of attack would be the serializer routines: presumably the first bytes from those would indicate what the type is.

Another approach is: cast the thing to an Object, and then inside your routine, have a cascade of something like:

B4X:
Sub DoSomething (subElement as Object, Action As String)

    If subElement Is Button Then
        Dim B as Button = Object    'cast object to its type
        B.Text = Action    'eg
    Else If subElement Is Label Then
        Dim L as Label = Object
        L.Text = Action
    Else If subElement Is Webview then
        Dim W as Webview = Object
        W.Request = Action & " " & URL    '???
    Else
        log("Unrecognized object type")
    End if

End Sub
I haven't actually run that code, but I'm sure it'll work third try :)
Thx, there is some promise in this approach. My intermediate workaround was to just declare all the GUI elements as B4XView. But that does not work with WebViews. Perhaps using your approach will enable me to combine both types into a single class.
 

emexes

Well-Known Member
Licensed User
Search the forums for GetAllViewsRecursive... queries about that are usually closely followed by queries (and examples) of casting between the generic Object type and specific object types :)
 

DawningTruth

Active Member
Licensed User
Ok tried it out. It worked. I had to set B to subElement.

Also note that you can return an Object too and it keeps the returned type that was entered.

B4X:
Sub DoSomething (subElement As Object, Action As String) As Object

   Dim returnObject As Object
   
   If subElement Is Button Then
       Dim B As Button = subElement    'cast object to its type
       B.Text = Action    'eg
   End If

   returnObject = B

   Return returnObject

End Sub
 
Top