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?
 

JordiCP

Expert
Licensed User
Longtime User
If you have / need a single code module :)
I have just experimented with it, and there is a way to work with multiple modules
  • Declare a code module with the same name as the library that you will compile
  • Declare as many classes as you want with NO internal vars and empty initializers (it is ok as they will behave as static classes). Populate them with as many Subs as you want. (better if Class_Globals and Initialize are declared as Private)
  • Declare an instance var of each class in the code module Process_Globals. Don't initialize them

Now you can compile it to a library and use it all directly in another project.
B4X:
   Log(testlibrary.aritmeticFunctions.add(2,3))
   Log(testlibrary.trigonometricFunctions.mySinD(45))
if you type "testlibrary." context menu will show available subs/members. If you type testlibrary.aritmeticfunctions.", context menu will show available subs in that class

Example project attached. Compile it to a library with the same name as the code module ("testlibrary")
 

Attachments

  • compileLibTest.zip
    3.8 KB · Views: 225
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
You're right. Although his second post is a bit different (at least as I understood it)
I know but it need to Initialize library,
I need use it without declare it.
Example when i enable library MyCamera then i use MyCamera.Take

I addressed this from a library approach (and you had already given the clue of how to use it in the same app with a code module)
For the rest, I'd blame it on @LucaMs :D
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
TBH I don't really understand the desire to be able to do it this way because (to me anyway) it's not a big deal to use a qualifier & only slightly more inconvenient to initialize an instance. Having said that, far be it for me to trivialize it - hence my effort to at least think about the possible options... :p

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
You're right. Although his second post is a bit different (at least as I understood it)


I addressed this from a library approach (and you had already given the clue of how to use it in the same app with a code module)
For the rest, I'd blame it on @LucaMs :D
Oh - I read that the other way round! :p I interpreted it as an example of what devmobile doesn't want to do!
upload_2017-7-27_8-12-58.png


- Colin.
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
I think this is what you are looking for:
Just don't use Class Globals definitions in your Class, compile It to a library, in a New project check your library in the libraries list, dim the class, and you can use its methods without need to initialize the Class.
Tip I use:
I created lots of libraries and custom views, so to find them quickly, the ones I use in all my projects I name them beggining with a ! so they appear att the top of the list. Example: "!MyFunctions".
 
Upvote 0
Top