Android Question Class array as property of other class

Homerclese

Member
Licensed User
Longtime User
I have a class, Account, with properties such as AccountID, Name, etc. One of its properties is Contacts() which is an array of Contact class. So it looks something like this:

B4X:
'Account class
Sub Class_Globals
    Public AccountID As Int
    Public Name As String
    Public Contacts() As Contact
End Sub

The Contact class contains typical FirstName, LastName, EmailAddress, etc, field. In my object model, an Account can have a variable number of Contacts.

So when I create a new object of type Account, I also need to append Contact class objects to the Account.Contacts() array. But I can't work out how to do this?
 

LucaMs

Expert
Licensed User
Longtime User
Wanting to be picky, you should not use public variables in that way, you should create some properties:
B4X:
'Account class
Sub Class_Globals
    Private mAccountID As Int
    Private mName As String
    ' ***  Private mContacts() As Contact  ' read below
End Sub

'Sets/Gets the Account ID
Public setAccountID(ID as Int)
    mAccountID = ID
End Sub
Public getAccountID As Int
    Return mAccountID
End Sub

'Sets/Gets the Account Name
...


Then you could use "mContacts As Map" instead of "mContacts() As Contact" and add some methods to your Account class:
B4X:
Public Sub setContacts(Contacts As Map)
    mContacts = Contacts
End Sub
Public Sub getContacts As Map
    Return mContacts
End Sub

Public Sub AddContact(NewContact As Contact, NewContactKey As Object)
    mContacts.Put(NewContactKey, NewContact)
End Sub

Public Sub GetContact(ContactKey As Object) As Contact
    Return mContacts.Get(ContactKey)
End Sub

The ContactKey can be an Int, so you can use the Map almost the same way as an array
 
Upvote 0

Homerclese

Member
Licensed User
Longtime User
Is it really necessary to create getter and setter methods instead of using public properties in B4A? It seems like a lot of work. Is there a shortcut to have the IDE template all of these for you, i.e. generate the getter/setter methods?

With regard to using a Map instead of an array, I guess I could also use a list?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Is it really necessary to create getter and setter methods instead of using public properties in B4A?
No, but it is a good practice; also, inside the two routines (set/get) you can validate the values.

Is there a shortcut to have the IDE template all of these for you, i.e. generate the getter/setter methods?
If I remember well, there is an external tool created by a member (@MarcoRome?)... I have to search for it

With regard to using a Map instead of an array, I guess I could also use a list?
Surely you can, but I think that a map, used that way, is better.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Homerclese

Member
Licensed User
Longtime User
Thanks Erel. My question is that I don't understand how to fill the array. It's just my lack of understanding of B4A. I'm coming from Xojo where I would write:

B4X:
Account.Contacts.Append newContact

I can't see how to append Contact class instances to an array of type Contact. Do I first have to know the dimension (i.e. number of contacts) and Dim the property?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…