Android Question When do I need "If SubExists"?

GuyBooth

Active Member
Licensed User
Longtime User
Looking at the 7.80 Beta release notes, it looks as if I can simplify my code:

Was:
B4X:
    If SubExists(module, Subname) Then CallSubDelayed(module,SubName)
Can now simply be:
B4X:
 CallSubDelayed3(module,SubName)
without throwing a runtime error.
Will it start the called module if it is not running, even if the Sub doesn't exist?

Is this also true with CallSub (and has it always been true)?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Will it start the called module if it is not running, even if the Sub doesn't exist?
It will start the activity or service if it is not currently running. If it is problematic then you need to check it with SubExists.

Is this also true with CallSub
CallSub never starts the target module.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
CallSub never starts the target module.

Sorry, yes I understand that. I meant this to be part of the first question - in other words, if I use the code "CallSub" and the sub doesn't exist, will it throw a runtime error?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
CallSub will throw an error if the target module is a class. In other cases it won't.

The reason behind it is that CallSub can be used for "duck type interfaces" where you have multiple classes with one or more identical methods.
B4X:
For Each o As Object In ListOfShapes
 CallSub(o, "DrawYourself")
Next
It is a programming mistake to add an object to this list that doesn't implement the DrawYourself sub.

Note that there is a special optimization for this usage (in release mode).
 
Upvote 0
Top