Android Question How to use library without dim!

cxbs

Active Member
Licensed User
Longtime User
I make each function into a custom library,
Then I want to call these libraries dynamically. I don't know how to operate them!

Normal practice:

b4a:
                Dim SFCG11 As SFCG11
                SFCG11.Initialize(Me,"Mold")
                Activity.AddView(SFCG11.GetBase,0,mLoadTop,100%x,mLoadHeight)
                SFCG11.show(ProgramID,mPara)

There are similar examples in the forum, but they have not been successful:

b4a:
    Dim jo As JavaObject
    Dim cls As String = "b4a.example.SFCG11"
    jo.InitializeStatic(cls)
    'mPanel=jo2.GetField("GetBase")
    mPanel=jo2.RunMethod("GetBase",Null)
 

JordiCP

Expert
Licensed User
Longtime User
I know it was answered long ago, but now I can't find it

Start a B4A project and add your custom functions to a code module (not a class module). For instance, you create a code module named myFunctions
myFunctions (code module):
Sub add(a As Int, b As Int) As Int
    Return a+b
End Sub

Sub multiply(a As Int, b As Int) As Int
    Return a*b
End Sub
Then compile it as a lib, giving it the name that you want, and it will be added to the additional libraries folder.

From your main project, just reference this library, and you can use the functions directly in the code, using moduleName.methodName syntax, and no need to declare nor initialize them in advance :)
B4X:
Sub mySub
    '...
    Dim c As Int = myFunctions.add(3,4)
    '...
End Sub
 
Upvote 0

cxbs

Active Member
Licensed User
Longtime User
I know it was answered long ago, but now I can't find it

Start a B4A project and add your custom functions to a code module (not a class module). For instance, you create a code module named myFunctions
myFunctions (code module):
Sub add(a As Int, b As Int) As Int
    Return a+b
End Sub

Sub multiply(a As Int, b As Int) As Int
    Return a*b
End Sub
Then compile it as a lib, giving it the name that you want, and it will be added to the additional libraries folder.

From your main project, just reference this library, and you can use the functions directly in the code, using moduleName.methodName syntax, and no need to declare nor initialize them in advance :)
B4X:
Sub mySub
    '...
    Dim c As Int = myFunctions.add(3,4)
    '...
End Sub

Thank you for your answer!

Because I have many custom libraries, I want to call them directly through the library name. I don't know if this can be realized

Due to the limited level of English, above from Google translation
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Just save each library with the same name as the code module inside it (in this case, myFunctions) and this will do the trick ;)
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
from your code, it looks like you are creating a custom view.

If that is the case then it is not really possible to directly create the view in code.

you should put the view on a layout and then load the layout and then display.

see this example from @Erel

 
Upvote 0

Albert Kallal

Active Member
Licensed User
Well, I can't say in a lot of cases, this road is all that valuable. I mean, I have a bunch of routines in MyCode.

So, from a global app point of view, MyCode.Sub works in all places. So there are two issues here. The first one being how can I use with easy in my code all over the place my utility code?

Then there is the separate issue of runtime resolution of code calls, and can I do this say by a name or value stuffed into a variable. So these two issues while related are in fact two different goals. Sharing and using code libraries is not necessary the result of the need to resolve sub/code calls by say a string variable name for such calls.

You can use CallSubDelayed("","Name of Sub as string goes here"). So this would and does give you some ability in this regards.

and if you are tried of having to create a instance of those classes? Then perhaps the need to write your code stuff as a class as compared to a standard code module should be a consideration? I in fact put of my code in my shared code libraries as standard subs, and don't bother to create as a class unless I REALLY do need a instance of that class (each with their own set of variables etc.).

R
Albert
 
Upvote 0
Top