Results 1 to 4 of 4

Thread: 'For Each' Collection and Classes problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    16

    'For Each' Collection and Classes problem

    Hello,
    I have distilled my problem down to the following simple example. Command0 button loads 3 employees into the collection. I expected Command1 button to display the 3 employees names, however what happens is that I get the last employee name displayed 3 times instead of each employee name displayed once. I must be doing something wrong, but I cannot see what, any help would be much appreciated. Thanks

    Form Code
    ---------
    Private cEmp As New cEmployee
    Private Ccoll As New Collection

    Private Sub Command0_Click()

    cEmp.Id = 1
    cEmp.FirstName = "Fred1"
    cEmp.LastName = "Bloggs1"
    Ccoll.Add cEmp, cEmp.FirstName

    cEmp.Id = 2
    cEmp.FirstName = "Fred2"
    cEmp.LastName = "Bloggs2"
    Ccoll.Add cEmp, cEmp.FirstName

    cEmp.Id = 3
    cEmp.FirstName = "Fred3"
    cEmp.LastName = "Bloggs3"
    Ccoll.Add cEmp, cEmp.FirstName

    MsgBox Ccoll.Count

    End Sub

    Private Sub Command1_Click()

    Dim Var

    For Each Var In Ccoll

    MsgBox Var.FirstName & " " & Var.LastName

    Next Var

    End Sub


    cEmployee Class
    ---------------
    Option Explicit

    Private m_LastName As String
    Private m_FirstName As String
    Private m_Id As String

    Property Get Id() As String
    Id = m_Id
    End Property

    Property Get LastName() As String
    LastName = m_LastName
    End Property

    Property Get FirstName() As String
    FirstName = m_FirstName
    End Property

    Property Let Id(ref As String)
    m_Id = ref
    End Property

    Property Let LastName(L As String)
    m_LastName = L
    End Property

    Property Let FirstName(F As String)
    m_FirstName = F
    End Property

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: 'For Each' Collection and Classes problem

    The reason you get the same name 3 times is because that instead of creating 3 different objects which you've added to the collection you've added the same object reference 3 times. This object reference is simply changed in the click event of Command0. To solve this remove the declaration of the cEmp object in the General Declaration section of your Form and then alter the Command0_Click event to code simular to this:
    VB Code:
    1. Private Sub Command0_Click()
    2. [b]    Dim cEmp As CEmployee
    3.     Set cEmp = New CEmployee [/b]
    4.     cEmp.Id = 1
    5.     cEmp.FirstName = "Fred1"
    6.     cEmp.LastName = "Bloggs1"
    7.     Ccoll.Add cEmp, cEmp.FirstName
    8.  
    9. [b]    Set cEmp = New CEmployee [/b]
    10.     cEmp.Id = 2
    11.     cEmp.FirstName = "Fred2"
    12.     cEmp.LastName = "Bloggs2"
    13.     Ccoll.Add cEmp, cEmp.FirstName
    14.  
    15. [b]    Set cEmp = New CEmployee [/b]
    16.     cEmp.Id = 3
    17.     cEmp.FirstName = "Fred3"
    18.     cEmp.LastName = "Bloggs3"
    19.     Ccoll.Add cEmp, cEmp.FirstName
    20.  
    21. [b]    Set cEmp = Nothing [/b]
    22.     MsgBox Ccoll.Count
    23. End Sub

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: 'For Each' Collection and Classes problem

    You where adding the same object 3 times to the collection. Each time you changed the objects FirstName and LastName property, the object in the collection was changed as well.
    I strongly advise against using the:
    Dim .. As New ...
    syntax.

    This way you don't have full control over when the object gets created or destroyed, and it creates overhead for Vb as well, because it must check if the object is already created, each time you use it.
    This should work:
    VB Code:
    1. Private cEmp As cEmployee
    2. Private Ccoll As Collection
    3.  
    4. Private Sub Command0_Click()
    5.  
    6. Set Ccoll = New Collection
    7.  
    8. Set cEmp = New cEmployee
    9. cEmp.Id = 1
    10. cEmp.FirstName = "Fred1"
    11. cEmp.LastName = "Bloggs1"
    12. Ccoll.Add cEmp, cEmp.FirstName
    13.  
    14. Set cEmp = New cEmployee
    15. cEmp.Id = 2
    16. cEmp.FirstName = "Fred2"
    17. cEmp.LastName = "Bloggs2"
    18. Ccoll.Add cEmp, cEmp.FirstName
    19.  
    20. Set cEmp = New cEmployee
    21. cEmp.Id = 3
    22. cEmp.FirstName = "Fred3"
    23. cEmp.LastName = "Bloggs3"
    24. Ccoll.Add cEmp, cEmp.FirstName
    25.  
    26. MsgBox Ccoll.Count
    27.  
    28. End Sub
    29.  
    30. Private Sub Command1_Click()
    31.  
    32. Dim Var
    33.  
    34. For Each Var In Ccoll
    35.  
    36. MsgBox Var.FirstName & " " & Var.LastName
    37.  
    38. Next Var
    39.  
    40. End Sub
    Frans

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    16

    Re: 'For Each' Collection and Classes problem

    Many thanks Joachin and Frans, I understand now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width