I can create an instance of class without to execute the myClass.initialize. Is this a correct behaviour ? I though for a new instance of any object, you need to run the initialize function.
Without to run the initialize, are you creating a static class ? Please explain with details that.
'Class Demo
Private Sub Class_Globals
Private xxxID As String
Private xxxCAS As String
Private xxxExpiry As Int
Private xxxContent As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Public Sub getID As String
Return xxxID
End Sub
Public Sub getCAS As String
Return xxxCAS
End Sub
Public Sub getExpiry As Int
Return xxxExpiry
End Sub
Public Sub getContent As String
Return xxxContent
End Sub
Public Sub Demo
Log("demo")
End Sub
from the Main module, I can call the following :
B4X:
Dim mDemo As clsDemo
mDemo.Demo
Without to execute the mDemo.initialize , the sub Demo executes (write on log the word demo)
Initialize - A class Object should be initialized before you can call any other Sub.
Initializing an Object Is done by calling the Initialize Sub.
When you call Initialize you set the Object's context (the parent activity or service).
See CLASS TUTORIAL...
It is true, the instance of the class works perfectly well without initializing it. Even properties (i'm trying b4a).
B4X:
'MyClass - Class module
Sub Class_Globals
Private mText As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Public Sub setText(Text As String)
mText = Text
End Sub
Public Sub getText As String
Return mText
End Sub
Public Sub WriteLog(Text As String)
Log(Text)
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
Private MyObj As MyClass
MyObj.WriteLog("Ciao")
MyObj.Text = "Erel"
MyObj.WriteLog(MyObj.Text)
Private MySecondObj As MyClass = MyObj
MySecondObj.WriteLog(MySecondObj.Text)
ExitApplication
End Sub
@avacondios You could test it, create two cases of your class, change something in one and see if it changes the other one, If it does its a static class.
eg,
B4X:
dim a as myclass ' with a variable text
dim b as myclass
a.text = "fred"
log(b.text)
if the log print 'fred', you know there is just one static class.
@avacondios You could test it, create two cases of your class, change something in one and see if it changes the other one, If it does its a static class.
eg,
B4X:
dim a as myclass ' with a variable text
dim b as myclass
a.text = "fred"
log(b.text)
if the log print 'fred', you know there is just one static class.
log "is empty". Therefore, it seems to me that, oddly enough, the initialization is not essential (probably it is done internally), but that the class is not static.
The instance is created when you dim the variable (unless you assign a new value immediately after you dim it). You should always initialize new instances.