Android Question CallSub

I'm having trouble calling the Initialize method of a B4A class passed as an object. I keep getting the "method not found" message.


Private Sub CreateInstance(TypeName As String, Params() As Object) As Object
Dim jo As JavaObject
Return jo.InitializeNewInstance(TypeName,Params).As(Object)
End Sub
....
Dim tp=GetType(baseOBJ)
Dim nuovoObj As Object = CreateInstance(tp,Null)
Log("GetType(oItem): " & GetType(nuovoObj))
If SubExists(nuovoObj, "Initialize") Then
CallSub(nuovoObj, "Initialize")
Else
Log("Metodo Initialize not found")
End If
 

Cableguy

Expert
Licensed User
Longtime User
without any code, I don't see how anyone can help.

But anyway, here's how it is usually done:

Dim myNewClassObject as ClassObject
myNewClassObject.initialize
 
Upvote 0
This is the class code.
I would like to use

Dim nuovaCassaO As tCassa
nuovaCassaO.Initialize
...
Dim baseOBJ as Object=nuovaCassaO

Dim tp=GetType(baseOBJ)
Dim nuovoObj As Object = CreateInstance(tp,Null)
CallSub(nuovoObj, "InitializeORM") <===== nothing happens here

'tCassa class
Sub Class_Globals
Public db_orm_fields() As String '= Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
Public db_orm_pks() As String '= Array As String("idCassa")
Public db_orm_tabname As String'="tCassa"
Public idCassa As Int
Public Nome As String
Public Int1 As String
Public Int2 As String
Public Int3 As String
Public Logo As String

End Sub
Public Sub InitializeORM()
db_orm_fields = Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
db_orm_pks= Array As String("idCassa")
db_orm_tabname ="tCassa"
End Sub
Public Sub Initialize()
InitializeORM
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png
 
Upvote 0
This is the class code.
I would like to use

B4X:
Dim nuovaCassaO As tCassa
nuovaCassaO.Initialize

...

Dim baseOBJ as Object=nuovaCassaO
Dim tp=GetType(baseOBJ)
Dim nuovoObj As Object = CreateInstance(tp,Null)
CallSub(nuovoObj, "InitializeORM")      <===== nothing happens here

tCassa CLASS:
'tCassa class

Sub Class_Globals
   Public db_orm_fields() As String '= Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
   Public db_orm_pks() As String '= Array As String("idCassa")
   Public db_orm_tabname As String'="tCassa"
   Public idCassa As Int
   Public Nome As String
   Public Int1 As String
End Sub

Public Sub InitializeORM()
   db_orm_fields  = Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
   db_orm_pks= Array As String("idCassa")
   db_orm_tabname ="tCassa"
End Sub

Public Sub Initialize()
   InitializeORM
End Sub
 
Upvote 0

teddybear

Well-Known Member
Licensed User
This is the class code.
I would like to use

Dim nuovaCassaO As tCassa
nuovaCassaO.Initialize
...
Dim baseOBJ as Object=nuovaCassaO

Dim tp=GetType(baseOBJ)
Dim nuovoObj As Object = CreateInstance(tp,Null)
CallSub(nuovoObj, "InitializeORM") <===== nothing happens here

'tCassa class
Sub Class_Globals
Public db_orm_fields() As String '= Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
Public db_orm_pks() As String '= Array As String("idCassa")
Public db_orm_tabname As String'="tCassa"
Public idCassa As Int
Public Nome As String
Public Int1 As String
Public Int2 As String
Public Int3 As String
Public Logo As String

End Sub
Public Sub InitializeORM()
db_orm_fields = Array As String("idCassa", "Nome", "Int1","Int2","Int3","Logo")
db_orm_pks= Array As String("idCassa")
db_orm_tabname ="tCassa"
End Sub
Public Sub Initialize()
InitializeORM
End Sub
I don't know why you want to create this class instance instead of directly using it.
For b4x class instance . if it is not initialized. the class methods will not be registered and you won't be able to call them by callsub.
such as
B4X:
Dim nuovaCassaO As tCassa
'nuovaCassaO.Initialize 'if you want to do CallSub it must be initialized
CallSub(nuovaCassaO, "InitializeORM")      '<===== if nuovaCassaO is not initialized. nothing will happen here
For your CreateInstance sub, you also need to run initialize method by javaobject.
 
Last edited:
Upvote 0
In this way

B4X:
Private Sub CreateInstance(TypeName As String, Params() As Object) As Object
    Dim jo As JavaObject
    jo= jo.InitializeNewInstance(TypeName,Params).As(Object)
    jo.RunMethod("_initialize",Null)
    Return jo
End Sub

I get this error

1762522774549.png
 
Upvote 0

teddybear

Well-Known Member
Licensed User
In this way

B4X:
Private Sub CreateInstance(TypeName As String, Params() As Object) As Object
    Dim jo As JavaObject
    jo= jo.InitializeNewInstance(TypeName,Params).As(Object)
    jo.RunMethod("_initialize",Null)
    Return jo
End Sub

I get this error

View attachment 168191
You need to pass the parameter BA to _initialize method.

B4X:
jo.RunMethod("_initialize",Array(getBA))
How to get the BA, you can see here
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
This is getting quite interesting.
the main questions are:
since we are delving deep under the skin of B4A, will this be supported in the future?
is it working in B4J also? (I think yes)
what is the corresponding implementation in B4i? (That’s important for cross platform projects)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Something is eluding me...
I now ask, why access a b4x class, created in b4x, by the coder, using java object INSIDE a b4x project!?
If it was a java object from the start, to witch the user had no other way of accessing it, i could understand, but this...
I just dont see why, what benefit
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Something is eluding me...
I now ask, why access a b4x class, created in b4x, by the coder, using java object INSIDE a b4x project!?
If it was a java object from the start, to witch the user had no other way of accessing it, i could understand, but this...
I just dont see why, what benefit
This is the what I asked at post#7
I don't know why you want to create this class instance instead of directly using it.
I guess the OP wants to create a function that can handle same method of different class instances
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
That’s a “class factory”, much like the Jserver in B4J, where you create handlers by just passing their class name
ok, so, then why not just initialize a new instance of the class in an handler, or just call a sub that would then do that... why trying to access it as javaobject?
I guess my real question is, what is the difference between a B4x Class Object and a "Class Factory"?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
So, I just had a quick chat with my pal, copilot...

From what he explains, this "Class Factory" would be better suited to create pluggins, as the class name doesn't need to be known beforehand, as oposed to B4X classes, where we create a instance of a known class.

This has been created (at least the barebones of such) by @Daestrum in his How to load a Class file at runtime Thread
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
ok, so, then why not just initialize a new instance of the class in an handler, or just call a sub that would then do that... why trying to access it as javaobject?
I guess my real question is, what is the difference between a B4x Class Object and a "Class Factory"?

it's quite a specific use case, but it's exactly what JServer does with AddHandler: you're writing a code that deals with classes that will be defined later
 
Upvote 0
Top