Home All Groups Group Topic Archive Search About

Help with VB6 Collection Classes

Author
17 Oct 2005 2:12 AM
DJ
Hello,

I'm a VB newbie trying to learn the ins and outs of collection classes. I'm
looking for help on how to check for an existing item in a VB6 collection
class. I think I need to add a function but but don't know where to place
the function; in the base class with the class properties (clsMsg800), or
the collection class with its methods (Msg800s) or the form's code?  I also
need to know how the function would be accessed.

Any ideas or suggestions would be appreciated. Thanks!

Base class clsMsg800
-------------------------------------------------------------------------------------
Option Explicit
Private m_MsgNumber As String
Private m_MsgType As String
Private m_MsgPriBitMap As String

Public Property Get MessageNumber() As String
    MessageNumber = m_MsgNumber
End Property

Public Property Let MessageNumber(ByVal NewMsgNumber As String)
    m_MsgNumber = NewMsgNumber
End Property

Public Property Get MessageType() As String
    MessageType = m_MsgType
End Property

Public Property Let MessageType(ByVal NewMessageType As String)
    m_MsgType = NewMessageType
End Property

Public Property Get MessagePriBitMap() As String
    MessagePriBitMap = m_MsgPriBitMap
End Property

Public Property Let MessagePriBitMap(ByVal NewMessagePriBitMap As String)
    m_MsgPriBitMap = NewMessagePriBitMap
End Property


Collection class Msg800s
-------------------------------------------------------------------------------------
Option Explicit
Private collMsg800s As Collection

Public Function add(ByVal MsgNumber As String, MsgType As String, _
    MsgPriBitMap As String) As clsMsg800

    Dim objMessage As New clsMsg800

    objMessage.MessageNumber = MsgNumber
    objMessage.MessagePriBitMap = MsgPriBitMap
    objMessage.MessageType = MsgType

    collMsg800s.add objMessage, MsgNumber

    Set add = objMessage

End Function

Public Sub Remove(ByVal Index As Variant)

    collMsg800s.Remove Index

End Sub

Public Function Count() As Long

    Count = collMsg800s.Count

End Function

Public Function NewEnum() As IUnknown

    Set NewEnum = collMsg800s.[_NewEnum]

End Function

Public Function Item(ByVal Index As Variant) As clsMsg800

Set Item = collMsg800s.Item(Index)

End Function

Private Sub Class_Initialize()

    Set collMsg800s = New Collection

End Sub

Author
17 Oct 2005 2:58 AM
MikeD
"DJ" <ddja***@tampabay.rr.com> wrote in message
news:wMD4f.153083$xl6.51809@tornado.tampabay.rr.com...
> Hello,
>
> I'm a VB newbie trying to learn the ins and outs of collection classes.
> I'm looking for help on how to check for an existing item in a VB6
> collection class. I think I need to add a function but but don't know
> where to place the function; in the base class with the class properties
> (clsMsg800), or the collection class with its methods (Msg800s) or the
> form's code?  I also need to know how the function would be accessed.


IMO

A collection class should have an Item property. This should be the default
property for the class. That way, the user of the collection can choose 1 of
2 syntaxes:

MyCollection.Item(index).Property

or

MyCollection(index).Property

This is just pretty much standard with collections.

If the item cannot be found within the collection, then the Item property
needs to return an "appropriate" value. For example, if the collection
stores objects, then the Item property should return Nothing if there is no
such item in the collection. If the collection stores something else, then
Item should return whatever value would be appropriate for what the
collection is storing (perhaps a 0-length string if the collection stores
strings, 0 if the collection stores numeric values; the problem is that
those could both be valid values for the item stored in the collection).
Generally speaking, you don't use collections for string or numeric data
types (primarily, collections are for storing objects), so maybe that's even
something you need to consider.  Since your collection appears to be for
only storing strings, an array might be better (perhaps a UDT of string
elements that is declared as an array).

In any case, the collection's Item property should return some value that
indicates if the item exists in the collection, and what that value is needs
to be documented so that other programmers (or even yourself for the future)
know what it is  From that point, it should be up to the calling code (ie.
the module using the collection) to check what the collection returns and
take appropriate action.

--
Mike
Microsoft MVP Visual Basic
Author
17 Oct 2005 4:06 AM
Ralph
Show quote Hide quote
"MikeD" <nob***@nowhere.edu> wrote in message
news:eDKrQas0FHA.1252@TK2MSFTNGP09.phx.gbl...
>
> "DJ" <ddja***@tampabay.rr.com> wrote in message
> news:wMD4f.153083$xl6.51809@tornado.tampabay.rr.com...
> > Hello,
> >
> > I'm a VB newbie trying to learn the ins and outs of collection classes.
> > I'm looking for help on how to check for an existing item in a VB6
> > collection class. I think I need to add a function but but don't know
> > where to place the function; in the base class with the class properties
> > (clsMsg800), or the collection class with its methods (Msg800s) or the
> > form's code?  I also need to know how the function would be accessed.
>
>
> IMO
>
> A collection class should have an Item property. This should be the
default
> property for the class. That way, the user of the collection can choose 1
of
> 2 syntaxes:
>
> MyCollection.Item(index).Property
>
> or
>
> MyCollection(index).Property
>
> This is just pretty much standard with collections.
>
> If the item cannot be found within the collection, then the Item property
> needs to return an "appropriate" value. For example, if the collection
> stores objects, then the Item property should return Nothing if there is
no
> such item in the collection. If the collection stores something else, then
> Item should return whatever value would be appropriate for what the
> collection is storing (perhaps a 0-length string if the collection stores
> strings, 0 if the collection stores numeric values; the problem is that
> those could both be valid values for the item stored in the collection).
> Generally speaking, you don't use collections for string or numeric data
> types (primarily, collections are for storing objects), so maybe that's
even
> something you need to consider.  Since your collection appears to be for
> only storing strings, an array might be better (perhaps a UDT of string
> elements that is declared as an array).
>
> In any case, the collection's Item property should return some value that
> indicates if the item exists in the collection, and what that value is
needs
> to be documented so that other programmers (or even yourself for the
future)
> know what it is  From that point, it should be up to the calling code (ie.
> the module using the collection) to check what the collection returns and
> take appropriate action.
>
> --
> Mike
> Microsoft MVP Visual Basic
>

Amplify MikeD's advice...

Public Property Get Item(vntIndexKey As Variant) As CMsg800
    On Error GoTo Bad_IndexKey
    Set Item = mCol(vntIndexKey)
    Exit Property
Bad_IndexKey:
    Set Item = Nothing
End Property

         ' in code somewhere, expanded for clarity
    Dim junk As CMsg800
    Set junk = col.Item(9)
    If junk Is Nothing Then
        Debug.Print "Junk is Nothing"
    Else
        Debug.Print "Junk is Ok"
    End If
Author
18 Oct 2005 1:50 AM
DJ
Ralph,

Thanks for clarifying Mike's suggestion. Your example helped clear things
up. Until I saw the example I had thought I set a default item property. I
was getting my methods and properties mixed up.

Thanks again!

~ Dave

Show quoteHide quote
"Ralph" <nt_consultin***@yahoo.com> wrote in message
news:BqadnfgmZtqSvs7eRVn-vA@arkansas.net...
>
> "MikeD" <nob***@nowhere.edu> wrote in message
> news:eDKrQas0FHA.1252@TK2MSFTNGP09.phx.gbl...
>>
>> "DJ" <ddja***@tampabay.rr.com> wrote in message
>> news:wMD4f.153083$xl6.51809@tornado.tampabay.rr.com...
>> > Hello,
>> >
>> > I'm a VB newbie trying to learn the ins and outs of collection classes.
>> > I'm looking for help on how to check for an existing item in a VB6
>> > collection class. I think I need to add a function but but don't know
>> > where to place the function; in the base class with the class
>> > properties
>> > (clsMsg800), or the collection class with its methods (Msg800s) or the
>> > form's code?  I also need to know how the function would be accessed.
>>
>>
>> IMO
>>
>> A collection class should have an Item property. This should be the
> default
>> property for the class. That way, the user of the collection can choose 1
> of
>> 2 syntaxes:
>>
>> MyCollection.Item(index).Property
>>
>> or
>>
>> MyCollection(index).Property
>>
>> This is just pretty much standard with collections.
>>
>> If the item cannot be found within the collection, then the Item property
>> needs to return an "appropriate" value. For example, if the collection
>> stores objects, then the Item property should return Nothing if there is
> no
>> such item in the collection.

>
> Amplify MikeD's advice...
>
> Public Property Get Item(vntIndexKey As Variant) As CMsg800
>    On Error GoTo Bad_IndexKey
>    Set Item = mCol(vntIndexKey)
>    Exit Property
> Bad_IndexKey:
>    Set Item = Nothing
> End Property
>
>         ' in code somewhere, expanded for clarity
>    Dim junk As CMsg800
>    Set junk = col.Item(9)
>    If junk Is Nothing Then
>        Debug.Print "Junk is Nothing"
>    Else
>        Debug.Print "Junk is Ok"
>    End If
>
>
>
Author
18 Oct 2005 12:03 AM
DJ
Mike,

Thanks for your reply. I have a few follow-up questions below.

Show quoteHide quote
>
> IMO
>
> A collection class should have an Item property. This should be the
> default property for the class. That way, the user of the collection can
> choose 1 of 2 syntaxes:
>
> MyCollection.Item(index).Property
>
> or
>
> MyCollection(index).Property
>
> This is just pretty much standard with collections.
>

Isn't the default Item property represented by the following (from my
original
message)?

    Public Function Item(ByVal Index As Variant) As clsMsg800
        Set Item = collMsg800s.Item(Index)
    End Function

Although not referenced in the code, I also set the default Procedure
Attribute for the collection class to Item.

> Generally speaking, you don't use collections for string or numeric data
> types (primarily, collections are for storing objects), so maybe that's
> even something you need to consider.

Again, that's what I thought I was doing (storing objects, that is) with the
following Msg800s method:

    Public Function add(ByVal MsgNumber As String, MsgType As String, _
        MsgPriBitMap As String) As clsMsg800

        Dim objMessage As New clsMsg800

        objMessage.MessageNumber = MsgNumber
        objMessage.MessagePriBitMap = MsgPriBitMap
        objMessage.MessageType = MsgType

        collMsg800s.add objMessage, MsgNumber

        Set add = objMessage
    End Function

Sure, the object is made up of simple strings, but still, it's an object
nonetheless, no?

>Since your collection appears to be for only storing strings, an array
>might be better (perhaps a UDT of string elements that is declared as an
>array).

From what I've read, collections are better suited when the number of items
to store is not known at runtime, yes? With an array I'd either have to wag
the number of items that need to be stored in the array at creation, or
redim it after I know the number of items. Correct?

>
> In any case, the collection's Item property should return some value that
> indicates if the item exists in the collection, and what that value is
> needs to be documented so that other programmers (or even yourself for the
> future) know what it is  From that point, it should be up to the calling
> code (ie. the module using the collection) to check what the collection
> returns and take appropriate action.

This is the part I needed assistance with. If you could shed some light here
I'd surely appreciate it.

>
> --
> Mike
> Microsoft MVP Visual Basic
>
>
>

If I've misunderstood any of your earlier comments, feel free to remedy my
misconceptions!

Thanks,

~ Dave
Author
18 Oct 2005 1:46 AM
DJ
Hi Mike,

> A collection class should have an Item property. This should be the
> default property for the class. That way, the user of the collection can
> choose 1 of 2 syntaxes:
>

Ralph's reply clarified your comment about having a default item method. In
my reply to your post I got my methods and properties mixed up. Please
disregard this portion of my reply.

Thanks!

~ Dave
Author
18 Oct 2005 3:56 AM
Ralph
Show quote Hide quote
"DJ" <ddja***@tampabay.rr.com> wrote in message
news:9uY4f.18581$ae.14191@tornado.tampabay.rr.com...
> Hi Mike,
>
> > A collection class should have an Item property. This should be the
> > default property for the class. That way, the user of the collection can
> > choose 1 of 2 syntaxes:
> >
>
> Ralph's reply clarified your comment about having a default item method.
In
> my reply to your post I got my methods and properties mixed up. Please
> disregard this portion of my reply.
>
> Thanks!
>
> ~ Dave
>

Actually I believe MikeD was suggesting that you ALSO set the Item()
property as the 'default'. That was in order to support, as he demo'd, two
syntaxes...
    MyCollection.Item(index).Property
    or
    MyCollection(index).Property

To do that you open the 'Dialog Box from Hell' aka Tools::Procedure
Attributes with your class file open.

-ralph
Author
17 Oct 2005 4:30 AM
mayayana
You can create an exists function by trying to retrieve
the item inside an error trap:

Public Function Exists(sKey as string) as Boolean
   Dim s2 as String  '-- assumes you have all string members.
     On Error Resume Next
        s2 = colMsg800s.Item(sKey)
        Exists = Err.Number = 0
End function

--
mayayanaX***@mindXXspring.com
(Remove Xs for return email.)
DJ <ddja***@tampabay.rr.com> wrote in message
Show quoteHide quote
news:wMD4f.153083$xl6.51809@tornado.tampabay.rr.com...
> Hello,
>
> I'm a VB newbie trying to learn the ins and outs of collection classes.
I'm
> looking for help on how to check for an existing item in a VB6 collection
> class. I think I need to add a function but but don't know where to place
> the function; in the base class with the class properties (clsMsg800), or
> the collection class with its methods (Msg800s) or the form's code?  I
also
> need to know how the function would be accessed.
>
> Any ideas or suggestions would be appreciated. Thanks!
>
> Base class clsMsg800
> --------------------------------------------------------------------------
-----------
> Option Explicit
> Private m_MsgNumber As String
> Private m_MsgType As String
> Private m_MsgPriBitMap As String
>
> Public Property Get MessageNumber() As String
>     MessageNumber = m_MsgNumber
> End Property
>
> Public Property Let MessageNumber(ByVal NewMsgNumber As String)
>     m_MsgNumber = NewMsgNumber
> End Property
>
> Public Property Get MessageType() As String
>     MessageType = m_MsgType
> End Property
>
> Public Property Let MessageType(ByVal NewMessageType As String)
>     m_MsgType = NewMessageType
> End Property
>
> Public Property Get MessagePriBitMap() As String
>     MessagePriBitMap = m_MsgPriBitMap
> End Property
>
> Public Property Let MessagePriBitMap(ByVal NewMessagePriBitMap As String)
>     m_MsgPriBitMap = NewMessagePriBitMap
> End Property
>
>
> Collection class Msg800s
> --------------------------------------------------------------------------
-----------
> Option Explicit
> Private collMsg800s As Collection
>
> Public Function add(ByVal MsgNumber As String, MsgType As String, _
>     MsgPriBitMap As String) As clsMsg800
>
>     Dim objMessage As New clsMsg800
>
>     objMessage.MessageNumber = MsgNumber
>     objMessage.MessagePriBitMap = MsgPriBitMap
>     objMessage.MessageType = MsgType
>
>     collMsg800s.add objMessage, MsgNumber
>
>     Set add = objMessage
>
> End Function
>
> Public Sub Remove(ByVal Index As Variant)
>
>     collMsg800s.Remove Index
>
> End Sub
>
> Public Function Count() As Long
>
>     Count = collMsg800s.Count
>
> End Function
>
> Public Function NewEnum() As IUnknown
>
>     Set NewEnum = collMsg800s.[_NewEnum]
>
> End Function
>
> Public Function Item(ByVal Index As Variant) As clsMsg800
>
> Set Item = collMsg800s.Item(Index)
>
> End Function
>
> Private Sub Class_Initialize()
>
>     Set collMsg800s = New Collection
>
> End Sub
>
>
Author
18 Oct 2005 1:52 AM
DJ
Mayayana,

Thanks for your input. Your suggestion worked!

~ Dave

Show quoteHide quote
"mayayana" <mayayanaX***@mindXXspring.com> wrote in message
news:2OF4f.17395$q1.13249@newsread3.news.atl.earthlink.net...
> You can create an exists function by trying to retrieve
> the item inside an error trap:
>
> Public Function Exists(sKey as string) as Boolean
>   Dim s2 as String  '-- assumes you have all string members.
>     On Error Resume Next
>        s2 = colMsg800s.Item(sKey)
>        Exists = Err.Number = 0
> End function
>
> --
> mayayanaX***@mindXXspring.com
> (Remove Xs for return email.)
> DJ <ddja***@tampabay.rr.com> wrote in message
> news:wMD4f.153083$xl6.51809@tornado.tampabay.rr.com...
>> Hello,
>>
>> I'm a VB newbie trying to learn the ins and outs of collection classes.
> I'm
>> looking for help on how to check for an existing item in a VB6 collection
>> class. I think I need to add a function but but don't know where to place
>> the function; in the base class with the class properties (clsMsg800), or
>> the collection class with its methods (Msg800s) or the form's code?  I
> also
>> need to know how the function would be accessed.
>>
>> Any ideas or suggestions would be appreciated. Thanks!
>>
>> Base class clsMsg800
>> --------------------------------------------------------------------------
> -----------
>> Option Explicit
>> Private m_MsgNumber As String
>> Private m_MsgType As String
>> Private m_MsgPriBitMap As String
>>
>> Public Property Get MessageNumber() As String
>>     MessageNumber = m_MsgNumber
>> End Property
>>
>> Public Property Let MessageNumber(ByVal NewMsgNumber As String)
>>     m_MsgNumber = NewMsgNumber
>> End Property
>>
>> Public Property Get MessageType() As String
>>     MessageType = m_MsgType
>> End Property
>>
>> Public Property Let MessageType(ByVal NewMessageType As String)
>>     m_MsgType = NewMessageType
>> End Property
>>
>> Public Property Get MessagePriBitMap() As String
>>     MessagePriBitMap = m_MsgPriBitMap
>> End Property
>>
>> Public Property Let MessagePriBitMap(ByVal NewMessagePriBitMap As String)
>>     m_MsgPriBitMap = NewMessagePriBitMap
>> End Property
>>
>>
>> Collection class Msg800s
>> --------------------------------------------------------------------------
> -----------
>> Option Explicit
>> Private collMsg800s As Collection
>>
>> Public Function add(ByVal MsgNumber As String, MsgType As String, _
>>     MsgPriBitMap As String) As clsMsg800
>>
>>     Dim objMessage As New clsMsg800
>>
>>     objMessage.MessageNumber = MsgNumber
>>     objMessage.MessagePriBitMap = MsgPriBitMap
>>     objMessage.MessageType = MsgType
>>
>>     collMsg800s.add objMessage, MsgNumber
>>
>>     Set add = objMessage
>>
>> End Function
>>
>> Public Sub Remove(ByVal Index As Variant)
>>
>>     collMsg800s.Remove Index
>>
>> End Sub
>>
>> Public Function Count() As Long
>>
>>     Count = collMsg800s.Count
>>
>> End Function
>>
>> Public Function NewEnum() As IUnknown
>>
>>     Set NewEnum = collMsg800s.[_NewEnum]
>>
>> End Function
>>
>> Public Function Item(ByVal Index As Variant) As clsMsg800
>>
>> Set Item = collMsg800s.Item(Index)
>>
>> End Function
>>
>> Private Sub Class_Initialize()
>>
>>     Set collMsg800s = New Collection
>>
>> End Sub
>>
>>
>
>