Android Question What is the default scope of a Sub?

Joacim

Member
Licensed User
Longtime User
I'm fairly new to B4A, or rather I've returned to it after being away from it for a few years. I was wondering what the default scope is for a Sub if you haven't specified it to be private or public? I'm also wondering how event handlers are really wired up? I can easily wire up events in the Designer but how would I do that if I wanted, for example, the TextChanged event for several EditText views to point to the same event handler?
 

KMatle

Expert
Licensed User
Longtime User
It's quite simple. The code behind (here the EditText) just calls a sub when "something" happens. Here it is a change of the text. The eventname is just added. At the end it's just a sub's name.

When you have several ET's the same sub can be called (same event name). Here you can use "sender" to get the object which has called the sub ("the event").

Subs can be private (internal only for the Editttext) or public to get/set something. If you create a library, you use subs which are privat and others which are accessable.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Thanks for the reply but I think you misunderstood my question. I do understand what events are, I was wondering how you wire them up, in the designer I can simply just pick the events I want to handle, but I can't make two EditText views call the same sub for the same event using it, so how do I do it?

I also wanted to know what the default scope of a sub is when you haven't specified the private or public modifier. Personally I don't like code that doesn't explicitly mention what scope they have, so I want to add the correct modifier myself, but I don't know what the default is.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but I can't make two EditText views call the same sub for the same event using it, so how do I do it?
Why not? Just set the event name to be the same. Later use Sender to distinguish between them.

The default scope is public, though there is a small difference in the way the warnings engine treats "unscoped subs". Good practice is to add Public to public subs.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
What I mean is that I can't do it from the designer, so how do I do it from code? Since the designer doesn't specify scope for event handlers, and the default scope is public does that mean that event handler has to be public?
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Thanks for the reply Erel, now about the event handlers, is there a way to specify the same event handler for multiple views using the designer or do I have to manually wire that up, in which case how do I do that? I'm sure I'm missing something very simple here.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Set a different Event name.

1593411672755.png

And if you duplicate the view, the event name is maintained.

You may have a look at the SecondProgram project in the B4X Getting started booklet.
 
Upvote 0

Joacim

Member
Licensed User
Longtime User
Aha, I misunderstood you earlier. So by just setting that property to the same string for more than one view, they will share the events, that's great. So if I understand this correctly the event handlers will magically have the Sender parameter even though the Sub doesn't specify that?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
So by just setting that property to the same string for more than one view, they will share the events,
Yes.

In the Event Sub
B4X:
Sub btnEvent_Click
    Private btnSender As Button
   
    btnSender = Sender
 
Upvote 0
Top