1

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 !

Community
  • 1
  • 1
TmSmth
  • 450
  • 5
  • 31
  • gsCartes is a property returning the collection, the actual variable that is holding it. The colCartes wont be visible from outside the class, due to private declaration – Nathan_Sav Feb 22 '17 at 09:51
  • Ok so if it was public i won't need the get and set ? And is `For Each cid In colCartesID.gsCartesID` correct ? – TmSmth Feb 22 '17 at 10:35
  • Yes, but it's good practice to do public accessors and private variables. The property let/get/set allow you to add some validation code. So if you allowed all but dates, you could say `if not(typename(value)="date") then varContents=value` to validate passing to a variant, varConents, but not allowing dates (not accurate VBA, just example) As that procedure is in the class, then it's acceptable to use the col, but making it private would still be of use in the class, but not visible to o/s the class, i.e. when using it. – Nathan_Sav Feb 22 '17 at 10:54
  • Ok got it, thanks ! – TmSmth Feb 22 '17 at 13:09

0 Answers0