Android Question Use global function

devmobile

Active Member
Licensed User
Hello
I need to create function in b4a that use it without use module name
Example IsPause is Built-in function.
Can i create this function?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I guess you could create a class containing the function, then compile it to a library.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Well in that case I don't think you can do it. As far as I know, the only ways are:

1) Create a class, compile it to a library then initialize an instance of the library to call the sub (eg: library.sub);
2) Create a class & declare a Public Sub, then initialize an instance of the class to call the sub (eg:class.sub);
3) Create a code module & declare a Public Sub, then call the sub from another module, activity or service (eg: module.sub);
4) Create an activity or service & declare a Public Sub, then call it from another module, activity or service (eg: CallSub(activity/service, "sub").

I don't think there's any way to create a globally visible sub & access it without a reference to the class, module, service or activity that it resides in. In fact, I can't think of a way of doing that in any of the languages I code in. Eg: even if you create an extension or singleton in Swift, you still have to use a reference to the class or type that it's declared in to access it.

Why do you need to access it like a built-in function?

- Colin.
 
Upvote 0

devmobile

Active Member
Licensed User
Well in that case I don't think you can do it. As far as I know, the only ways are:

1) Create a class, compile it to a library then initialize an instance of the library to call the sub (eg: library.sub);
2) Create a class & declare a Public Sub, then initialize an instance of the class to call the sub (eg:class.sub);
3) Create a code module & declare a Public Sub, then call the sub from another module, activity or service (eg: module.sub);
4) Create an activity or service & declare a Public Sub, then call it from another module, activity or service (eg: CallSub(activity/service, "sub").

I don't think there's any way to create a globally visible sub & access it without a reference to the class, module, service or activity that it resides in. In fact, I can't think of a way of doing that in any of the languages I code in. Eg: even if you create an extension or singleton in Swift, you still have to use a reference to the class or type that it's declared in to access it.

Why do you need to access it like a built-in function?

- Colin.
I love built-in function.
I dont want to use module.sub
Actually I want to use sub directly,maybe?
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I love built-in function.
I dont want to use module.sub
Actually I want to use sub directly,maybe?

The only way to do that is to declare the sub in the same module as you are calling it from - but you would still need to use module.sub to call it from outside that module (as far as I know).

- Colin.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The only way to do that is to declare the sub in the same module as you are calling it from - but you would still need to use module.sub to call it from outside that module (as far as I know).
Code modules can not have events. Code modules is most probably not the right place.
Service, Activity or Classes. I would like to suggest to create a class.
Actually I want to use sub directly,maybe?
Add the sub to your activity if you want to use it directly. If you want to use the sub from another activity; add the code to this acivity TOO.
If you want to get rid of not using all the code multiple times: WRITE A CLASS and use this Class in all your activities or Services.
There is no way to make it a build-in method.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Code modules can not have events. Code modules is most probably not the right place.
Service, Activity or Classes. I would like to suggest to create a class.

I think you mean code modules can't handle events - but devmobile was talking about functions, the equivalent of which are subs in B4X. A class may or may not be the right place for this, depending on what he actually wants to do. If it's simply calling a utility function that will do some processing, then a code module is probably better. If he wants to also encapsulate data (member variables, etc...) &/or have multiple instances, then a class would be the way to go.

- Colin.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Sometimes I seemed to see libraries that did not need initialization and that you could call their routines directly.
There was a similar question some months ago but now I can't find it.

As said here, there is no direct way (that I know of) to "extend" language without a "." in the call. But there is no need to instantiate/initialize a variable if you compile a library with a code module.
For instance, you name your code module "myUtils", and add some functions to it "function1" and "function2". Then compile it into a library.
In your other project just reference the library, and can directly use
B4X:
Dim a as int=3
Dim b as int=4
myUtils.function1(a,b)     '<-- no need to instantiate (nor initialize) anything before.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Using a routine directly can also be a problem: you do not know which library contains that routine!

In the case of a code module compiled as library you do not know the name of the code module.

Have to write something like:

MyLibrary.DoSomething

lets you know (when you will read your source code after years) which library exposes DoSomething.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Using a routine directly can also be a problem: you do not know which library contains that routine!

In the case of a code module compiled as library you do not know the name of the code module.

Have to write something like:

MyLibrary.DoSomething

lets you know (when you will read your source code after years) which library exposes DoSomething.
That's right. The trick is to compile the library with the same name as the code module ;)
 
Upvote 0
Top