array of classes or combining 2 classes into one?

raytronixsystems

Active Member
Licensed User
Longtime User
I'm not sure really what to do here- use a list or create 2 separate classes to accomplish this:

I want to create 2 classes in my project- one called Person which defines the Person class and another called People which defines a class containing many People - maybe up to 100 or so. I have tried a few things but run into a problem as I will explain in the code fragment below:

class module 1(person.bas):
B4X:
'Class module
Sub Class_Globals
    Private FirstName, LastName As String
    Private Age As int
End Sub

Public Sub Initialize ...

End Sub

------

class module 2 (people.bas):


'Class  module
Sub Class_Globals
    Private MAXPEOPLE As Int: MAXPEOPLE = 100
    Private People(MAXPEOPLE) As Person ' array of person classes
End Sub


Public Sub Initialize (...) 
End Sub

'In module 2 I cannot reference individual fields of the Person class from the 'array because they are defined as "private". 
'if I try to reference the 'name field from the first entry in the array as follows it cannot be accessed:

Public Sub GetFirstName (idx AS Int) As String

      return People(idx).FirstName

EndSub

'however,
'if I declare the Person Class in the following manner all works fine:


class module 1(person.bas): 

'Class module
Sub Class_Globals
    Dim FirstName, LastName As String
    Dim Age As int
End Sub

Public Sub Initialize ...

End Sub

AI would like to leave the fields in the Person Class Private because I might want to deal with this class individually outside of the Persons Class. If I use get...set methods in the Person Class how can I put them to use with the People Class?

I'm confused, What should I do? All of the examples of class definitions here have the individual elements in a class declared as private not public as I have done in order to get this to work.

Is this called subclassing? Not really sure because just learning OOP here.


Regards,
Ray
 

stevel05

Expert
Licensed User
Longtime User
As you've made the fields in the person class private, you'll need to create get/set methods to retrieve and update data to those class fields. You can't access it directly as you are trying to do.
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
thx,
How would I access the 1st' Person's Info from the array?assuming I have the 3 methods referenced below written for the Person Class.
Something like this?

B4X:
dim idx As Int
dim Firstname As String
dim LastName As String
din Age As Int

idx = 0



FirstName =  = People(idx). GetFirstName
LastName = People(idx). GetLastName
Age = People(idx).GetAge

I might as well code it now because I did as much work tying the explanation as I might have doing the coding!:sign0161:


I'll let you know how It worked out.

Regards,
Ray
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
That looks OK not having tried it. If you get a problem, I can have a look later.

The key is creating a public sub in the person class to return the data.
 
Upvote 0
Top