Nested Class Examples

macnlkc

Member
Licensed User
Longtime User
Anyone have any good nested class examples that would be good for a newbie to understand?

:sign0104:
 

Jaames

Active Member
Licensed User
Longtime User
I'm assuming that you want to achieve something like this :
B4X:
Person.putFirstName("James").putLastName("Bond").putPhoneNumber("007 007-007").PrintPersonToTheLog

If so, it can be done like this:

You create class cPerson, like bellow
B4X:
'Class module
Sub Class_Globals
    Private mFirstName As String
    Private mLastName As String
    Private mPhoneNumber As String
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

Sub putFirstName(fName As String)As cPerson
    mFirstName = fName
    Return Me
End Sub
  
Sub putLastName(sName As String)As cPerson
    mLastName = sName
    Return Me
End Sub

Sub putPhoneNumber(phoneN As String)As cPerson
    mPhoneNumber = phoneN
    Return Me
End Sub

Sub PrintPersonToTheLog
    Log($"First Name ${mFirstName}
Surname : ${mLastName}
Phone Number : ${mPhoneNumber}"$)      
End Sub

And then, when you need it, call it like I posted on the top.
For Example , in the Activity_Create Sub:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim Person As cPerson
    Person.Initialize
    Person.putFirstName("James").putLastName("Bond").putPhoneNumber("007 007-007").PrintPersonToTheLog
End Sub
Log Output:
B4X:
** Activity (main) Create, isFirst = true **
First Name James
Surname : Bond
Phone Number : 007 007-007
** Activity (main) Resume **
I don't know if this is correct way to do it, it could be totally wrong, but it works, I guess Erel can elaborate on this and tell us...
 
Upvote 0
Top