Based on this post What are the benefits of using Classes in VBA? I've made a bit of code. The first class CarteID
Option Explicit
Private agePersonne As Long
Private nomPersonne As String
Public Property Get glAgePersonne() As Long
glAgePersonne = agePersonne
End Property
Public Property Let glAgePersonne(lAgePersonne As Long)
agePersonne = lAgePersonne
End Property
Public Property Get glNomPersonne() As String
glNomPersonne = nomPersonne
End Property
Public Property Let glNomPersonne(lNomPersonne As String)
nomPersonne = lNomPersonne
End Property
Then the second class ProcessCarteID
Option Explicit
Private colCartesID As Collection
Public Property Get gsCartesID() As Collection
Set gsCartesID = colCartesID
End Property
Public Property Set gsCartesID(sCartesID As Collection)
Set colCartesID = sCartesID
End Property
Function RecupAgeMoyen() As Double
Dim cid As CarteID
Dim moyenneAge As Double
moyenneAge = 0
For Each cid In colCartesID
moyenneAge = moyenneAge + cid.glAgePersonne
Next cid
moyenneAge = moyenneAge / colCartesID.Count
RecupAgeMoyen = moyenneAge
End Function
And the module :
Option Explicit
Function PopulateArray() As Collection
Dim colInfos As New Collection
Dim cid As CarteID
Set cid = New CarteID
cid.glNomPersonne = "Fred"
cid.glAgePersonne = 21
colInfos.Add cid
Set cid = New CarteID
cid.glNomPersonne = "Julie"
cid.glAgePersonne = 18
colInfos.Add cid
Set cid = New CarteID
cid.glNomPersonne = "Jean"
cid.glAgePersonne = 25
colInfos.Add cid
Set PopulateArray = colInfos
End Function
Sub TestAgeMoyen()
Dim pci As ProcessCarteID
Set pci = New ProcessCarteID
Set pci.gsCartesID = PopulateArray()
Debug.Print pci.RecupAgeMoyen()
End Sub
In the function RecupAgeMoyen()
I tried For Each cid In colCartesID
and For Each cid In gsCartesID
. Both worked.
I just wanted to know why it worked also with gsCartesID
because I am not sure to understand.
Thanks !