Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New Guy Needs Help - Collections

Status
Not open for further replies.

JMTS

Programmer
Oct 31, 2000
26
US
I am currently working with an application the uses User Defined Type. We are breaking our app. out into DLL's. You cannot pass user defined type so I need to create collections. Can you have a collection within a collection?

For instance I have collection called UpDtRec it has Database Name, Table Name, ListCount, as fields. I also have a field that is called FldName. I can have multiple FldName entries, so I want to define FldName as it's own collection within UPDtRec. Does that make since? Thanks.

Jason
 
Yes. I recently worked on a project for a 911 center.
The equipment belonged to the firemen, and the firemen to the truck, and the truck to the company, and the company to the county....
I had a collection within a collection within....
and the list go's on.
Define your object first, then add it to your collection.
Then add your collection to the second collection.

I hope this helped.
The door to life is never locked, but few have the knowledge to open it.
 
Create a class that has the same properties as your user definded type. Then you only have minor changes to make to your code. The only thing you have to change is how you declare it, and you have to instantiate the object. When you're done with it, you need to rememeber to set it equal to Nothing. If you need an example of how to do this, post a portion of your user definded type, and I'll convert it to a class and show you how to use it. Snaggs
tribesaddict@swbell.net
If a parsley farmer is sued, can they garnish his wages?
 
JMTS -

Another cool trick is to create a let/get property called "RawData" or I've sometimes seen it called "ToStream". What it does is convert the interior contents of your class to a big long string that you can then pass around quite easily. On the other end, you parse the string apart and set the individual values back.

If you want to be using the latest "thing", you could do it via XML (instantiate a DOMDocument object and jam your values into it). This would produce a fairly industry-standard string. But be aware of the performance penalty of using XML (it's rather wordy).

Chip H.
 
Snaggs,

This my user defined type:

Type typUpdateRecord
ListCount As Integer
UpDateFlag As Integer
DbName As String
TbName As String
GuiCtl(65) As Integer
FldName(65) As String
FldValue(65) As String
WhereClause As String
End Type
Private strupdaterecord As typUpdateRecord

This is what I did in my collection class:

Public Function Add(DbName As String, ListCount As Integer, TblName As String, UpDateFlag As Integer, DataFields As colFields, WhereClause As String, Optional sKey As String) As clsUpData
'create a new object
Dim objNewMember As clsUpData
Set objNewMember = New clsUpData


With objNewMember

.DbName = DbName
.ListCount = ListCount
.TblName = TblName
.UpDateFlag = UpDateFlag
.WhereClause = WhereClause
.DataFields

End With

If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If


'return the object created
Set Add = objNewMember
Set objNewMember = Nothing


End Function

DataFields is the collection that is below. So I fill in the Datafield collection and then pass it in to the above collection. Is that a good way to do this?





Public Function Add(FldName As String, FldValue As Variant, GuiCtl As Integer, Optional sKey As String) As clsFields
'create a new object
Dim objNewMember As clsFields
Set objNewMember = New clsFields


With objNewMember
.FldName = FldName
.FldValue = FldValue
.GuiCtl = GuiCtl
End With

If Len(sKey) = 0 Then
mCol.Add objNewMember
Else
mCol.Add objNewMember, sKey
End If


'return the object created
Set Add = objNewMember
Set objNewMember = Nothing


End Function
 
JMTS -

What you've done looks pretty good. You can add some functionality that will allow you to do a FOR..EACH loop on your collection.

What you do is add this code:
[tt]
Public Function Item(ByVal Index As Variant) As clsFields
[tab]Set Item = mCol.Item(Index)
End Function

Public Property Get NewEnum() As IUnknown
[tab]Set NewEnum = mCol.[_NewEnum]
End Property
[/tt]
The only trick is that for the NewEnum property, you need to change the procedure ID to -4 (Tools menu, Procedure Attributes, click the Advanced button).

You can then instantiate your collection class and loop through it just as if it were a VB collection.

Chip H.
 
Chip,

Question for you... I have seen this trick with setting the Procedure ID to -4 and have used it several times. Does anyone know why or how this works? I've selected each one of the items in the Procedure ID thinking that one of them was the same as -4, but I could never get it to work, unless I typed in the -4. This is even documented in the MSDN help, but it doesn't say why it works or what the significance of it is.

Anyone? Snaggs
tribesaddict@swbell.net
There are two kinds of people in life: people who like their jobs, and people who don't work here anymore.
 
I suspect it (in conjunction with the name "NewEnum" and the IUnknown type) matches up to a special COM constant. I've never investigated further (lack of time, and a lack of interest in COM's inner workings)

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top