How do i make a global function?

So in my app so far there is a nav bar but i am having to add a nav bar function_click ever time i create a new module, how do i make it so the nav bar will appear in every module and the _click will happen in every module.

Thanks, James.
 
So i added a simple test:

B4X:
'Class module
Sub Class_Globals
   Dim testers As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   testers = 1
   
End Sub

How do i link this into my main module so that i can have testers in a msgbox.

Thanks, James.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Something like this. Call the sub GetValue. Add this class code to your project. Dim it in the main Activity like:

Dim Test As Tester 'Or whatever you name it
MsgBox(Test.GetValue, "My Test")

B4X:
'Class module
Private Sub Class_Globals    
     Private testers As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
     testers = 1
End Sub
Public Sub GetValue As Int
     Return testers
End Sub
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Got to agree with these guys but .....

If all you want is a simple little global function which doesn't need to manipulate any views then if you just create a new code module.

Project > Add New Module > Code Module

Let's call it testModule

then make a new Sub in that like this for example:

B4X:
Sub testSub(i As Int)
   ToastMessageShow("You gave me this number " & i & " what should I do with it?", True)
End Sub

Then you can call it from any other module and not only that but when you write out the first part which is testModule. after you hit the dot you get intellisense!

So from my main module I could do:

testModule.testSub(25)

I think Objects is definitely the better way to go but just thought I'd mention this :)

If you want to get fancy do this in testModule:
B4X:
Sub testSub(i As Int) As String 'ooh, now it returns a string!
   ToastMessageShow("You gave me this number " & i & " what should I do with it?", True)
   Return("Ok, did that!")
End Sub

and this in main:
B4X:
ToastMessageShow(testModule.testSub(25), True)
 
Last edited:
Upvote 0
Something like this. Call the sub GetValue. Add this class code to your project. Dim it in the main Activity like:

Dim Test As Tester 'Or whatever you name it
MsgBox(Test.GetValue, "My Test")

B4X:
'Class module
Private Sub Class_Globals    
     Private testers As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
     testers = 1
End Sub
Public Sub GetValue As Int
     Return testers
End Sub

Hi i am getting this error for some reason:

Error description: Object expected.
Occurred on line: 42
Msgbox(test.GetValue, "My Test")
Word: getvalue

In My Activity Module:
B4X:
Sub level3button_Click
Dim test As Int

Msgbox(test.GetValue, "My Test")
End Sub

In my class module:
B4X:
'Class module
Private Sub Class_Globals    
     Private testers As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
     testers = 1
End Sub
Public Sub GetValue As Int
     Return testers
End Sub
 
Upvote 0
This is not correct.

In My Activity Module:
B4X:
Sub level3button_Click
Dim test As Int

Msgbox(test.GetValue, "My Test")
End Sub

You should dim test as the name of your class. If your class is named mytest in your project/IDE, you should use:

B4X:
 Dim test As mytest

So i have done what you have said but i think its just a small thing i am still not understanding. My class module is called "navigation" and the activity module is called "levelmenu".


Navigation class module:
B4X:
'Class module
Private Sub Class_Globals    
     Private testers As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
     testers = 1
End Sub
Public Sub GetValue As Int
     Return testers
End Sub

Level menu activity module:
B4X:
Sub level3button_Click
Dim testers As navigation

Msgbox(testers.GetValue, "My Test")
End Sub

Thanks, James.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The code below is what you have. Because this is a class you need to Initialize the object. I would put that in the globals sub like:

NOTE: You also need B4A version 2.0+

B4X:
'Put this in the main activity
Sub Globals 
     Dim testers As navigation
     testers.Initialize
End Sub


B4X:
'Your code below
Sub level3button_Click
     'Remove this from your sub -->Dim testers As navigation
     Msgbox(testers.GetValue, "My Test")
End Sub
 
Upvote 0
Thanks i have now got the msgbox working. What i would ultimately like to do is call a design but what function should i use seeing as its a class module.

Thanks, James.
 
Upvote 0
Top