PC Review


Reply
Thread Tools Rate Thread

Adding class object to collection repeats same object through collection?

 
 
Erazmus
Guest
Posts: n/a
 
      17th Sep 2007
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.

 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      17th Sep 2007
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)


"Erazmus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>


 
Reply With Quote
 
Erazmus
Guest
Posts: n/a
 
      17th Sep 2007
On Sep 17, 1:35 pm, "Chip Pearson" <c...@cpearson.com> wrote:
> 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)
>
> "Erazmus" <eraz...@ihug.co.nz> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > 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.- Hide quoted text -

>
> - 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a collection as a particular type of object or a particular class Ronald Dodge Microsoft Excel Programming 1 16th Nov 2007 09:11 AM
Collection object fails within class FP1 Microsoft Access Form Coding 9 22nd Aug 2007 08:42 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
Adding Name object to a collection does not add an object Tim Richardson Microsoft Excel Programming 5 8th Oct 2006 01:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:30 AM.