Adding class object to collection repeats same object through collection?

E

Erazmus

Hi Guys,

I have an issue with the following code -

I have worked this together from examples found on this site, the
class indexing is thanks to Chip Pearson see groups topic "Using an
index with a class module?"

Public bulkloadFile As clBulkloadfile

....

Sub testing()

Dim currentField As clBulkloadField
Set currentField = New clBulkloadField
Dim index As Integer

If bulkloadFile Is Nothing Then
Set bulkloadFile = New clBulkloadfile
End If

For index = 1 To 12

With currentField
.contents = "Hi : " & index
.name = tyStatementDetail
.label = "Hi-Ho"
End With

bulkloadFile.add newField:=currentField

MsgBox "Index = " & index & vbCrLf & _
"Current Field" & vbCrLf & _
" Contents: " & currentField.contents & vbCrLf & _
" Label: " & currentField.label & vbCrLf & _
"bulkload file(index)" & vbCrLf & _
" Contents: " & bulkloadFile(index).contents & vbCrLf &
_
" Label: " & bulkloadFile(index).label & vbCrLf

''
*******************************************************************************************
'' The MsgBox above produces the expected results.
''
*******************************************************************************************

Next

For index = 1 To bulkloadFile.count
MsgBox "Index = " & index & " Contents = " &
bulkloadFile(index).contents
Next

''
***************************************************************************************************************
'' The MsgBox above produces the same thing 12 times, ie. 12x the
last entry from the first loop.
''
***************************************************************************************************************

Set bulkloadFile = Nothing

End Sub

As I'm fairly new to the whole creating and using my own of classes,
it's probably something obvious that I'm missing, would anyone know
where I should start looking?

Any help appreciated.

Cheers,
Deon.
 
C

Chip Pearson

What type of object is clBulkloadfile? Is it a class that implements a
collection?

I think the root of your problem is that you are not creating new instances
of clBulkloadField within the For loop. If you expect to have 12 separate
instances of this class, you must create each instance and store that
instance in a Collection or Dictionary. The following code illustrates this.

Sub AAA()
Dim C As clBulkloadField
Dim Index As Long
Dim Coll As Collection

Set Coll = New Collection

' load 'em up
For Index = 1 To 12
Set C = New clBulkloadField
C.name = CStr(Index)
Coll.Add C
Next Index

' read 'em out
For Index = 1 To 12
Debug.Print Coll(Index).name
Next Index
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
E

Erazmus

What type of object is clBulkloadfile? Is it a class that implements a
collection?

I think the root of your problem is that you are not creating new instances
of clBulkloadField within the For loop. If you expect to have 12 separate
instances of this class, you must create each instance and store that
instance in a Collection or Dictionary. The following code illustrates this.

Sub AAA()
Dim C As clBulkloadField
Dim Index As Long
Dim Coll As Collection

Set Coll = New Collection

' load 'em up
For Index = 1 To 12
Set C = New clBulkloadField
C.name = CStr(Index)
Coll.Add C
Next Index

' read 'em out
For Index = 1 To 12
Debug.Print Coll(Index).name
Next Index
End Sub

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consultingwww.cpearson.com
(email on the web site)






















- Show quoted text -

This is the exported clBulkloadfile.cls -

VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "clBulkloadfile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private myBulkloadFile As Collection

Public Sub add(newField As clBulkloadField)
myBulkloadFile.add item:=newField
End Sub

Public Property Get item(index As Integer) As clBulkloadField
Attribute item.VB_UserMemId = 0
Set item = myBulkloadFile(index)
End Property

Public Property Get count() As Integer
count = myBulkloadFile.count
End Property

Private Sub Class_Initialize()
Set myBulkloadFile = New Collection
End Sub

Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = myBulkloadFile.[_NewEnum]
End Property

*****************************************************************************************************************************
I'm trying to learn by copying / adapting so this should be similar to
code that you've previously posted so please excuse my ignorance :)

My understanding of what I thought I was writing was -

I'd created a class called clBulkloadField which validates the
contents field once three key properties are set.

I'd then created the clBulkloadfile which was a collection of
clBulkloadFields ( I visualise as a type of dynamic array of
fields ). I chose this based on the example given in the "Using an
Index with a class module" post as the end result is nice and
readable. Later I would like to move on to something like -

For Each field in bulkloadfile
'' do some things here with any invalid fields
next

And this seemed like the best way.

Again, this may be more a case of not understanding what I was doing
100%, I thought the original post under "using an Index with a class
module" was fairly straight forward and that I couldn't muck it up too
much but I've proved myself wrong :p

Cheers,
Deon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top