|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with VB6 Collection ClassesI'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 "DJ" <ddja***@tampabay.rr.com> wrote in message IMOnews: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. 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
Show quote
Hide quote
"MikeD" <nob***@nowhere.edu> wrote in message Amplify MikeD's advice...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 > 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 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 > > > Mike,
Thanks for your reply. I have a few follow-up questions below. Show quoteHide quote > Isn't the default Item property represented by the following (from my > 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. > 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 Again, that's what I thought I was doing (storing objects, that is) with the > types (primarily, collections are for storing objects), so maybe that's > even something you need to consider. 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 From what I've read, collections are better suited when the number of items >might be better (perhaps a UDT of string elements that is declared as an >array). 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? > This is the part I needed assistance with. If you could shed some light here > 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. I'd surely appreciate it. > If I've misunderstood any of your earlier comments, feel free to remedy my > -- > Mike > Microsoft MVP Visual Basic > > > misconceptions! Thanks, ~ Dave Hi Mike,
> A collection class should have an Item property. This should be the Ralph's reply clarified your comment about having a default item method. In > default property for the class. That way, the user of the collection can > choose 1 of 2 syntaxes: > my reply to your post I got my methods and properties mixed up. Please disregard this portion of my reply. Thanks! ~ Dave
Show quote
Hide quote
"DJ" <ddja***@tampabay.rr.com> wrote in message Actually I believe MikeD was suggesting that you ALSO set the Item()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 > 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 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 -- Show quoteHide quotemayayanaX***@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 > > 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 >> >> > > |
|||||||||||||||||||||||