Thanks but Wouldn't this also work ?
Public Sub Initialize
Language = "English"
End Sub
Public Sub Initialize2(language_translation As String, some_extra_parameter As Double)
'
Language = language_translation
'.....
End Sub
I mean what is the benefit of
Private Sub InternalInitialize
Dim r As Reflector
r.Target = Me
r.Runmethod4("innerInitialize", Array As Object(r.GetProcessBA("main")), Array As String("anywheresoftware.b4a.BA"))
End Sub
It would seem that it would work, but in fact, though in this specific case no error would be raised, the Class variables, declared in
Class_Globals would not be declared nor defined.
Imagine you have such a class (MyClass say):
'Class MyClass
Sub Class_Globals
Private Language As String = "English"
Private Length As Double = 1.50
End Sub
Public Sub Initialize
Log("--Start Initialize Method--")
Log(Language)
Log(Length)
Log("--End Initialize Method--")
End Sub
Public Sub Initialize2(ba As Object, pLanguage As String, pLength As Double)
InternalInitialize(ba)
Log("--Start Initialize2 Method--")
Log(this.Language)
Log(this.Length)
Log("--End Initialize2 Method--")
'These are local variables, and not the class variables
Language = pLanguage
Length = pLength
End Sub
If you Initialize the class with this code:
Dim c As MyClass
c.Initialize
The output is as expected:
--Start Initialize Method--
English
1.5
--End Initialize Method--
Because the
Class_Globals, among other things, gets called when Initialize is invoked.
However, if you try this
Dim r As Reflector
Dim c As MyClass
c.Initialize2(r.GetActivityBA, "French", 1.5)
The ouput will be:
--Start Initialize2 Method--
0
--End Initialize2 Method--
because Class_Globals was not called. Furthermore, the variables that you think are class variables, are in fact local variables that'll disappear as soon as the sub exists. As mentioned in my first post there's only one initializer: Initialize.
However if you call InternalInitialize within Initialize2 as I mentioned in my original post, the class will be initialized correctly and the output for MyClass.Initialize2 will be correct.
I have updated my original post to clarify what Initialize does.
If you want absolute proof that calling InternalInitialize is the fix to that issue, try this:
Sub Class_Globals
Private Language As String = "English"
Private Length As Double = 1.50
Private this As MyClass = Me
End Sub
Public Sub Initialize
Log("--Start Initialize Method--")
Log(this.Language)
Log(this.Length)
Log("--End Initialize Method--")
End Sub
Public Sub Initialize2(ba As Object, pLanguage As String, pLength As Double)
'InternalInitialize(ba)
Log("--Start Initialize2 Method--")
Log(this.Language)
Log(this.Length)
Log("--End Initialize2 Method--")
'These are local variables, and not the class variables
Language = pLanguage
Length = pLength
End Sub
Private Sub InternalInitialize(ba As Object)
Dim r As Reflector
r.Target = Me
r.Runmethod4("innerInitialize", Array As Object(ba), Array As String("anywheresoftware.b4a.BA"))
End Sub
And you'll get that exception raised in Initializer2:
--Start Initialize Method--
English
1.5
--End Initialize Method--
--Start Initialize2 Method--
myclass_initialize2 (java line: 62)
java.lang.NullPointerException: Attempt to read from field 'java.lang.String b4a.example.myclass._language' on a null object reference
at b4a.example.myclass._initialize2(myclass.java:62)
at b4a.example.main._activity_create(main.java:328)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Uncomment InternalInitialize and no exception will be raised and the output will be correct.