I have seen many discussions in the B4A forum, but nothing seemed to match what I am curious to know. To make reusable code, I like to use Class objects for a lot of things. In one of my apps, the Class is initialized including a reference to the Parent.
This is a crude mock up skeleton of the question's code:
Things I have thought of:
1) Pass the myUserID as a param in every Sub in the MyObject class
2) Create a UserID in MyObject and set it once during Initialize so all Subs can get it.
#2 seems like the obvious choice, but what if the value was something dynamic? Like, something the user has picked from a list. I would want the value from the Main at the moment of the Class Sub, not the original value passed during Initialize.
So now the question: Can an initialized object get to any of the public vars in the parent?
This is a crude mock up skeleton of the question's code:
B4X:
'Main
Sub Process_Globals
Public App As Application
Public NavControl As NavigationController
Public MainPage As Page
Public myObject As SomeObject
Public myUserID As Int : myUserId = 1123
End Sub
Sub Application_Start (Nav As NavigationController)
NavControl = Nav
NavControl.ToolBarVisible = False
NavControl.NavigationBarVisible = False
SomeObject.Initialize(Me, "John", "Doe")
End Sub
Sub CompletedAction
Log("Action complete")
End Sub
B4X:
'Class MyObject
Sub Class_Globals
ParentModule As Object
FirstName, LastName As String
CreatedBy As Int
End Sub
Sub Initialize(mParentModule As Object, mFirstName As String, mLastName As String)
ParentModule = mParentModule
FirstName = mFirstName
LastName = mLastName
CreatedBy = ParentModule.myUserID '<<<< does not work
ParentModule.ActionComplete '<<<< does not work
CallSub(ParentModule, "ActionComplete") '<<<< this works
End Sub
End Class
Things I have thought of:
1) Pass the myUserID as a param in every Sub in the MyObject class
2) Create a UserID in MyObject and set it once during Initialize so all Subs can get it.
#2 seems like the obvious choice, but what if the value was something dynamic? Like, something the user has picked from a list. I would want the value from the Main at the moment of the Class Sub, not the original value passed during Initialize.
So now the question: Can an initialized object get to any of the public vars in the parent?