title: VB6: How do I properly store class objects in a collection?
body: <p>I'm looking at a legacy VB6 app, and trying to understand how VB6 Collections work. Using the <em>Collection.Add</em> method, I am finding that the collection just exists of its last added option, repeated. <em>e.g. if I add 1,2,3,4,and 5 to a collection, I will get 5, 5, 5, 5, and 5 back as the collection contents.</em></p>
  
  <p>In my test case, I have an encapsulation class module, <strong>EncapsulationClass.cls</strong> that stores some simple strings. Its implementation:</p>
  
  <pre><code>Option Explicit
  
  'ivars
  Private pEntityId As String
  Private pEntityName As String
  
  'properties
      'pEntityId
      Public Property Get entityId() As String
          Let entityId = pEntityId
      End Property
  
      Private Property Let entityId(ByVal inEntityId As String)
          Let pEntityId = inEntityId
      End Property
  
  
      'pEntityName
      Public Property Get entityName() As String
          Let entityName = pEntityName
      End Property
  
      Private Property Let entityName(ByVal inEntityName As String)
          Let pEntityName = inEntityName
      End Property
  
  
  'constructor
      Public Sub init(ByVal inEntityId As String, ByVal inEntityName As String)
          Let entityId = inEntityId
          Let entityName = inEntityName
      End Sub
  </code></pre>
  
  <p>I want to store several instances of these in an iterable object, so I used a <em>Collection</em>.</p>
  
  <p>In my test case, I have this simple function:</p>
  
  <pre><code>Private Function getACollection() As Collection
      Dim col As New Collection
      Dim data(0 To 5) As String
      data(0) = "zero"
      data(1) = "one"
      data(2) = "two"
      data(3) = "three"
      data(4) = "four"
      data(5) = "five"
  
      For Each datum In data
          Dim encap As New EncapClass
          encap.init datum, datum &amp; "Name"
          col.Add encap
      Next
  
      'return
      Set getACollection = col
  End Function
  </code></pre>
  
  <p>This function is then used in the following simple logic:</p>
  
  <pre><code>Private Sub Form_Load()
      Dim col As Collection
      Set col = getACollection()
  
      For Each i In col
          Debug.Print i.entityId, i.entityName
      Next i
  End Sub
  </code></pre>
  
  <p>I would expect the output to be:</p>
  
  <pre><code>one oneName
  two twoName
  three threeName
  four fourName
  five fiveName
  </code></pre>
  
  <p>However, instead, I simply get a repetition of the last element added, five times. </p>
  
  <pre><code>five fiveName
  five fiveName
  five fiveName
  five fiveName
  five fiveName
  </code></pre>
  
  <p>Is there something I'm missing, syntactically? Looking through various <a href="http://books.google.com/books?id=kVqB5hjNsScC&amp;pg=PA361&amp;lpg=PA361&amp;dq=object%20in%20collection%20%22visual%20basic%206%22&amp;source=bl&amp;ots=18_vsvRT-J&amp;sig=lim7SXDoYkO-J-LS_niX9qIse5c&amp;hl=en&amp;sa=X&amp;ei=n5zeUqu-H-Of2QWo04HoBw&amp;ved=0CD0Q6AEwAg#v=onepage&amp;q=object%20in%20collection%20%22visual%20basic%206%22&amp;f=false" rel="nofollow">books</a>, collections are appended with the Add method, and work as expected.</p>