deltaquatro,
Well, I like chocolate!
The Private variable mcTemplate is superfluous, I took from this one of my
apps and forgot that bit.
The NewEnum() As IUnknown is the way that we can enumerate our collection
class, just as you can a standard collection.It is a bit of a frig that I
got from VB6 many years ago.
Hakyab,
that should also answer your questions 3 & 4.
On the first, does it really work? I can see that you can get away without
the terminate, garbage collection will take care of that, but the
initialize? How does the collection get setup without that?
On the second, I never thought of that, as I always add the Item anyways.
But it wouldn't work, because if you look at the code for the Users class,
you will see an Item property. That is how we emulate the collection
methods, but you have to call it here.
--
HTH
Bob
"deltaquattro" <(E-Mail Removed)> wrote in message
news:294f7220-3702-4c28-9820-(E-Mail Removed)...
Ciao, Bob,
eh, eh, we thought of the same thing to solve my issue of yesterday.
Thanks for the reply! You're answering all of my questions lately.
I'll have to make you a present for Easter

Uhmmm, your code works
(as always), and running it gave me a funny surprise

However, I
really don't understand this part:
Private mcTemplate As String
Function NewEnum() As IUnknown
Set NewEnum = mcUsers.[_NewEnum]
End Function
I guess I'll have to study some reference: can you point me to a good
book/tutorial on this aspects of OOP in VBA?
Best Regards
deltaquattro
On 17 Mar, 11:48, "Bob Phillips" <bob.phill...@somewhere.com> wrote:
> I was going to suggest yesterday that you went this way :-).
>
> First, you have two classes, one for the object, one for the collection.
> This is example code for these two classes for a User object.
>
> 'User class =================================================
> Option Explicit
>
> Private mcName As String
> Private mcGender As String
> Private mcRole As String
>
> Private Sub Class_Initialize()
> End Sub
>
> Public Property Get Name() As String
> Name = mcName
> End Property
> Public Property Let Name(Value As String)
> mcName = Value
> End Property
>
> Public Property Get Gender() As String
> Select Case mcGender
> Case "M": Gender = "Male"
> Case "M": Gender = "Female"
> End Select
> End Property
> Public Property Let Gender(Value As String)
> mcGender = Value
> End Property
>
> Public Property Get Role() As String
> Role = mcRole
> End Property
> Public Property Let Role(Value As String)
> mcRole = Value
> End Property
>
> 'Users class ================================================
> Option Explicit
>
> Private mcUsers As Collection
> Private mcTemplate As String
>
> Function NewEnum() As IUnknown
> Set NewEnum = mcUsers.[_NewEnum]
> End Function
>
> Public Function Add(ByRef mUser As User)
> mcUsers.Add mUser, mUser.Name
> End Function
>
> Public Property Get Count() As Long
> Count = mcUsers.Count
> End Property
>
> Public Property Get Items() As Collection
> Set Items = mcUsers
> End Property
>
> Public Property Get Item(Index As Variant) As User
> Set Item = mcUsers(Index)
> End Property
>
> Public Sub Remove(Index As Variant)
> mcUsers.Remove Index
> End Sub
>
> Private Sub Class_Initialize()
> Set mcUsers = New Collection
> End Sub
>
> Private Sub Class_Terminate()
> Set mcUsers = Nothing
> End Sub
>
> Not that I also provide you wa way to loop through the collection, just
> like
> in VBA. This is code that demonstrates how to use it
>
> Sub MyUsers()
> Dim AllUsers As Users
> Dim ThisUser As User
> Dim usr As User
>
> Set AllUsers = New Users
>
> Set ThisUser = New User
> ThisUser.Name = "Bob"
> ThisUser.Gender = "M"
> ThisUser.Role = "Helper"
> AllUsers.Add ThisUser
>
> Set ThisUser = New User
> ThisUser.Name = "delquattro"
> ThisUser.Gender = "M"
> ThisUser.Role = "Helper"
> AllUsers.Add ThisUser
>
> For Each usr In AllUsers
>
> Debug.Print usr.Name & " is a " & usr.Gender & " " & usr.Role
> Next usr
>
> Set ThisUser = Nothing
> Set AllUsers = Nothing
>
> End Sub
>
> --
>
> HTH
>
> Bob
>
> "deltaquattro" <deltaquat...@gmail.com> wrote in message
>
> news:b03cb05e-f40c-4725-ad93-(E-Mail Removed)...
>
> > Hi,
>
> > I would like to add a collection in my class: naturally I would like
> > to keep all the automagic methods of the Collection object (Add,
> > Count, etc.). For now I did it like this:
>
> > 'Class Module
> > Public ErrorString As Collection
>
> > 'Class Initialization Method
> > Private Sub Class_Initialize()
> > Set ErrorString = New Collection
> > End Sub
>
> > but I know that using Public properties is Not A Good Thing. How could
> > I do this with a Private collection? Can you show me how to rewrite
> > the Add, Count, etc. methods? I tried to do that myself but it didn't
> > work. For example, if I write
>
> > Public Function Add(Error As String)
> > ErrorString.Add String
> > End Sub
>
> > I cannot see .ErrorString.Add among the properties of my class! What
> > am I doing wrong? Thanks,
>
> > Best Regards
>
> > deltaquattro