B4J Question [SOLVED] Confirming A Class Has a Sub Before Attempting to Call It

cklester

Well-Known Member
Licensed User
I have this code:

B4X:
Private Sub clv_MenuButtons_ItemClick (Index As Int, Value As Object)
    Try
        pnLayoutLoader.RemoveAllViews
        CallSub2(Value,"Show",pnLayoutLoader)
        lblDashboardTitle.Text = "App - " & CallSub(Value,"GetName")
    Catch
        Log("clicked on stub")
    End Try
End Sub

I don't want to *.RemoveAllViews until I know that I can show something in its place. At the moment, it seems to run *.RemoveAllViews, then, if it tries the CallSub2() and the class doesn't have a "Show" sub, it skips to the catch, thus leaving the display blank.

How do I verify a class has a sub prior to attempting to call it?

THE ANSWER

OK, so, before I submitted this question, I did a search with the term "How do I verify a class has a sub prior to attempting to call it". No helpful results (at least on the first page).

Then I went to my code, and on the Value class, I started typing possible sub names for what I wanted. I started with "Exists," and, lo and behold, "SubExists" was available!

Here's the revised code that works like I want:

B4X:
Private Sub clv_MenuButtons_ItemClick (Index As Int, Value As Object)
    If SubExists(Value,"Show") Then
        pnLayoutLoader.RemoveAllViews
        CallSub2(Value,"Show",pnLayoutLoader)
        lblDashboardTitle.Text = "App - " & CallSub(Value,"GetName")
    Else
        Log("clicked on stub")
    End If
End Sub

Looks like I don't need you guys anymore. 😁 🤣 😂 <-- that's a joke!!!

Anyway, posting this in case someone else has those search terms in the future... :)😇
 
Top