|
-
May 22nd, 2006, 05:23 AM
#1
Thread Starter
Junior Member
'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
-
May 22nd, 2006, 06:29 AM
#2
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:
Private Sub Command0_Click()
[b] Dim cEmp As CEmployee
Set cEmp = New CEmployee [/b]
cEmp.Id = 1
cEmp.FirstName = "Fred1"
cEmp.LastName = "Bloggs1"
Ccoll.Add cEmp, cEmp.FirstName
[b] Set cEmp = New CEmployee [/b]
cEmp.Id = 2
cEmp.FirstName = "Fred2"
cEmp.LastName = "Bloggs2"
Ccoll.Add cEmp, cEmp.FirstName
[b] Set cEmp = New CEmployee [/b]
cEmp.Id = 3
cEmp.FirstName = "Fred3"
cEmp.LastName = "Bloggs3"
Ccoll.Add cEmp, cEmp.FirstName
[b] Set cEmp = Nothing [/b]
MsgBox Ccoll.Count
End Sub
-
May 22nd, 2006, 06:31 AM
#3
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:
Private cEmp As cEmployee
Private Ccoll As Collection
Private Sub Command0_Click()
Set Ccoll = New Collection
Set cEmp = New cEmployee
cEmp.Id = 1
cEmp.FirstName = "Fred1"
cEmp.LastName = "Bloggs1"
Ccoll.Add cEmp, cEmp.FirstName
Set cEmp = New cEmployee
cEmp.Id = 2
cEmp.FirstName = "Fred2"
cEmp.LastName = "Bloggs2"
Ccoll.Add cEmp, cEmp.FirstName
Set cEmp = New cEmployee
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
-
May 22nd, 2006, 06:53 AM
#4
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|