Collections Of Collections?
Is it possible to have a collection of collections? I.e. A collection of objects, where one of the private member properties of each object is another collection of objects.
I figured it would be possible, but have been having trouble implementing, so i just wanted to make sure.
TYIA
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Enumrating Collections Of Collections Of Classes
Hi Guys, I know someone here will know the answer I am looking for, basically I'm setting up the enumeration for my custom class structure. I will start by describing the structure, and what I have already done finishing with the problem, a varitable cornicopia of intrigue!!!
I have a Standard class (using real world pseudo examples of cource), lets call this class cLeaf, it has some properties but nothing out of the ordinary.
Now i have created a Collection class called cBranch, as you would expect it holds a collection of cLeaf's and a few other non descript properties and methods. I have set up this class to enumerate properly through its cLeaf's.
Finally there is a 'top level' collection class called cTree, this has a collection of cBranch's and again a load of methods and stuff. I have set up the enumeration in this class to enumerate all the cBranch classes.
The problem, I would like to be able to iterate through each cLeaf within the entire cTree collection, from the cTree class. In other words I need to set up a kind of 'double enumerator' whereby a call like this one:
Code:
Dim TestLeaf as cLeaf
For each TestLeaf in cTree
'process
Next
would cause each leaf from each branch within the tree to be enumerated. How do I do this? I understand the concepts of Enumeration basically and have so far used this syntax:
Code:
Public Property Get NewEnum() As IUnknown
Set NewEnum = mCol.[_NewEnum]
End Property
But everything else I have found concerning this issue seemed needlessly complex (I am refering to extracts of HardCore Visual Basic and stuff on www.mvps.org). Can anyone shed some light on my plight??
Thanking all of you just for your interest in this thread!! If any of you can post an informative reply or even better the solution I will bow down in divine worship (virtually) !!!!
ZuluWarrior
Collections
Hello,
Is there a quick easy way to clear out all the contents of a collection?
Collections
I have a question about collections, and specifically the forms collection.
When are forms added and/or removed from the forms collection? Are they added when the form is loaded into memory (formname.show), and are they removed when a form is unloaded? I'm working on an MDI application and I want to be able to to go through the forms collection like this:
Code:
For each form in Forms
If form.name <> someformname.frm Then
unload form
End If
Next Form
Is this valid code? FYI: I'm using Visual Basic 6.0 If someone could answer these questions for me I'd appreciate it.
Not Collections But...
i'm lost and out of my realm.
I want to set up something, but don't know what it's called.
Lets say i have a variable (or object?) called Family
the family has members (many)
each member has characteristics like haircolor, age, eyecolor, height, etc
i want to be able to reference things in the family like this:
family.item(0).age = 2
bobsEyeColor = family.item(3).eyecolor
How do I do this?
Collections
Edit by Moderator:
Re: http://www.xtremevbtalk.com/showthread.php?t=89151
Here is another solution to the above mentioned problem. I know that this thread should be long closed, but I could not find a solution anywhere, so I wanted to post this here just in case some one else is searching.
When adding values to a collection, you are adding a pointer to another storage location. If you are adding directly from a ADO recordset, then the data is lost when you close the conneciton. The pointer is no longer pointing to any storage location.
To solve this problem, it is as easy as saving the recordset value into a temporary variable and then adding to the collection using the temp location.
Code:
Dim TempProcess As String
TempProcess = rs!myprocessid
UserCollection.Add TempProcess
I hope this helps somebody.
BDS
Edit by Moderator:
Per the Posting Guidelines, threads over 1 month old are considered archive-only.
Collections
I've found these VB forums extremely valuable to solve problems. This is my first posting and I had a question regarding collections which I think I solved while putting the question together. Not sure if this is appropriate, but I am posting anyway with my analysis and solution below....hopefully helpful to someone else (I spent a full day and a half trying to figure out the problem). Again, I am new to posting on the forums...please let me know if this is inappropriate and apologies in advance if it is
1. The problem I was posting
**********************************
I am having difficulty using the Collections object. Invariably, after I add items to the object (through a subroutine), I can get the count property but when I try to access the items i get an error
Run-time error 3420 : Object is no longer valid
some detail
I have a user defined type that contains a collections object
Public Type GType
GID As String
Versions As Integer
Sg As String
Description As String
PP As Collection
End Type
Dim G as GType
I have tried a variety of ways to fill the collection by calling a subroutine, even passing the whole type
Call GetPP(G)
Public Sub GetPP(G as GType)
Dim rs as New ADODB.Recordset
Query="Select PP from PPTable"
rs.open, Query, con
Set G.PP = New Collection
do until rs.eof
G.PP.Add rs!PP
loop
rs.close
End Sub
Back at the main code i try
msgbox G.PP.Count
and I get a message with the proper count
but when i try to look at an item
msgbox G.PP.Item(1)
I get the Object is no longer valid message
*************************************
2. Solution
The collection object will hold other objects. By storing the recordset item ie. rs!PP, it didn't store the text value in the collection, but an object. After I closed the recordset (rs.close), this object was no longer available
so, instead I copied the text value of the rs item into a string, then stored that into the collection
PPtemp=rs!PP
G.PP.Add PPtemp
when I do this, then the error does not occur and I can see my collection items
Great...now back to work!
LarryH
Collections?
Does anyone know where I can find a good tutortial/book (or something) on collections? Something that starts simple and works its way into the topic quite a bit.
Collections
I have read and re-read about "collections" but I still do NOT understand it. Can any explain this to me in a simple idiotic way or refer me to a good resource?
More specifically, I do not understand this.
----------
Private mstrProductCode As String
Public Property Get ProductCode () As String
ProductCode = mstrProductCode
End Property
Public Property Let ProductCode (ByVal strProductCode As String)
mstrProductCode = strProductCode
End Property
-------------
thanks ahead for any help!
Collections
Ok, I downloaded Collections in Visual Basic which I believe Thinker wrote up about making your own collection.
I've been tried to model my collection from your example, however I have one problem that has got me stumped.
Somewhere in your sample program of MyObjectCollection you must initialize the Count property to zero. Or something does this.
When you start up the program in your form_load subroutine colMyObjCol.Count = 0 after creating this object.
Private Sub Form_Load()
Set colMyObjCol = New MyObjects
End Sub
However when I create my object I keep getting colMyObjCol.Count = <Variable not Defined>
What did I leave out? I can't seem to figure this out? I've got the NewEnum in my collection with the Procedure ID = -4 as you stated in your explaination of your example.
Collections
i'm using a collection to keep track of paths to files (to mp3s). i can use statements like
'add first element then remove it
SongTable.add "d:asong.mp3"
SongTable.remove 1
let's say i have a collection of 10 songs - i want toi explicitly set a couple of them (or swap a couple items). when i use the following statement i get an err
SongTable(2)="testy poo"
i get "object required" error. so how can i explicitly modify the contents of an item?
Collections
is there a way to know all the controls in a frame. this will be to reset the textboxes to "" instead of doing it one by one.
i know there's me.controls but there's no me.frame1.controls.....
I'll be among the best soon, very soon!!!
Collections
i have a book here, and trying to learn how to use collection, however, when i use the code they give me ... i get "can't assign to read-only property" when i run the test sub (actual err line shown in bold, uderline, and italic
any help would be GREAT
the code is:
Code:
Option explicit
dim anEmployee as new employee
Sub Test()
set anemployee = new employee
anemployee.name ="bill gates"
msgbox anemployee.name
CLASS FILE "Employee"
Code:
option explicit
private fname as string
public property get name() as string
name = fname
end property
Collections
Hi,
I´ve following problem:
I get a collectionlist, wich includes objects from a selfdefined class. These object gets properties, wich are strings.
For example:
z.B. class: clsperson
Objekt: objperson
Eigenschaft: objperson.vorname
The Problem is now - I want to do a comparison between new objects, that should be added to the collection and the collectionobjects itself. but i don´t know how to call back the objectproperties from the objects of the collection, to do a StrComp.
Please help ...
Frank
Collections
When I set a Collection of Objects to nothing
Set MyCollection = Nothing
I was wondering if the Objects within the Collection are Terminated by default or if I have to iterate through the Collection and set each Object equal to Nothing
OOP: Collections
''''my code so far... IT ALL WORKS .
Code:
'-------------------------------form
Private Sub Form_Load()
[b]Dim ref As Variant[/b] ???????
dim main_item As Citem
Set main_item = New Citem
main_item.item = 1000
[b][i]set[/i] ref = main_item.addOp(CStr(txtOP), CStr(txtwc))[/b]
End Sub
'---------------------------------item class
Private m_item As Integer
Public Operations As New Collection
Property Get item() As Variant
item = m_item
End Property
Property Let item(ByVal vNewValue As Variant)
m_item = vNewValue
End Property
Private Sub Class_Initialize()
Set Operations = New Collection
End Sub
Private Sub Class_Terminate()
Set Operations = Nothing
End Sub
[b]Public Function addOp(opNum, WC) As Coperations
Dim tempOp As New Coperations
tempOp.oper_num = opNum
tempOp.work_center = WC
main_item.Operations.Add tempOp, opNum
Set addOp = tempOp
End Function[/b]
'----------------------------------operations class
Private m_oper_num As Integer
Private m_work_center As String
Public Materials As New Collection
Property Get oper_num() As Variant
oper_num = m_oper_num
End Property
Property Let oper_num(ByVal vNewValue As Variant)
m_oper_num = vNewValue
End Property
Property Get work_center() As Variant
work_center = m_work_center
End Property
Property Let work_center(ByVal vNewValue As Variant)
m_work_center = vNewValue
End Property
Private Sub Class_Initialize()
Set Materials = New Collection
End Sub
Private Sub Class_Terminate()
Set Materials = Nothing
End Sub
--------------------------------------------------------------------------------
Help With UDT's And Collections Please
Hi All
I Recently ran into the following error when trying to create a collection of unique User Defined Types. Can anyone explain what the error means? What is the work around to get the structure I am after?
Thanks,
~Tom
'Compile Error:
Only user-defined types defined in public object modules can be coerced to or from a variant or passed to a late-bound functions'
Code:
Option Explicit
Public Type DrillType
Operation As Byte
X As Double
Y As Double
ToolNo As Byte
End Type
Public Type MillType
Operation As Byte
XB As Double
YB As Double
XE As Double
YE As Double
ConRad As Double
FeedRate As Double
ToolOffset As Byte
ToolNo As Byte
End Type
Public gEvents As New Collection
Sub main()
Dim DrillEvent As DrillType
Dim MillEvent As MillType
DrillEvent.Operation = 100
DrillEvent.X = 2.25
DrillEvent.Y = 2.125
DrillEvent.ToolNo = 1
MillEvent.Operation = 101
MillEvent.XB = 2.25
MillEvent.YB = 2.125
MillEvent.XE = 2.25
MillEvent.YE = 2.125
MillEvent.ConRad = 0
MillEvent.FeedRate = 10
MillEvent.ToolOffset = 2
MillEvent.ToolNo = 1
gEvents.Add DrillEvent '<--- Errors out here
gEvents.Add MillEvent
End Sub
Collections
How can I know if a specific object is already inside my collection ?
It gives an error when I ask for an inexisting key.
Thanks in advance
Collections
I read the document on collections. They seem useful, but I do not see the advantage of customizing a collection? I am going to use a collection to store players, and the player will need a collection or an array to store the items he has. And I also need to be able to save this info to the hard drive. All this is making my head hurt any ideas?
Collections
Must all collections reference objects that are in class modules? I have been doing a little reading and I am not sure if I can just create a collection within my main and just iterate thru it?
At any rate, I get an object not set variable when I run the following. I think I *may be* confused on how this works.
Sub Main()
Dim myClaimForms As New Collection
Dim myobject As Object
myClaimForms.Add ("test1")
myClaimForms.Add "test2"
Debug.Print "count = " & myClaimForms.Count
For Each myboject In myClaimForms
Debug.Print myobject.Item
Next
End Sub
Collections
Ok, I have the following:
VB Code:
Dim c(1 To 2) As New CollectionPrivate Sub Form_Load() c(1).Add "aa", "a" c(1).Add "bb", "b" c(2).Add "cc", "c" c(2).Add "dd", "d"End Sub
Now I would like to loop though the collections msgboxing their Item and Key.
The following is close to what I want:
VB Code:
Dim i As Integer For i = 1 To UBound(c) For Each x In c(i) MsgBox x Next Next
It msgboxes all the Items, what do I need to alter to msgbox their keys too? Maybe collections aren't what I am looking for?
Collections
is the bottom index in a collection 1 or 0
does option explicit have an effect on that
what does option explicit do anyways
Collections
I'm having great difficulty using collections.
I created two classes, a student class that has LastName and StudentID then I created a StudentCollection class to hold an array of student classes.
my add function in StudentCollection looks like this -
Code:
Public Sub Add(ByVal aclsStudent As Cstudent)
mcolStudentCollection.Add aclsStudent, aclsStudent.StudentID
End Sub
in main here is the add function (mclsStudents is a module level variable predefined in the form_load):
Code:
Private Sub cmdAddStudent_Click()
Dim mclsStud As Cstudent
Set mclsStud = New Cstudent
If (Me.txtID) = "" Then
MsgBox "Invalid ID"
Me.txtID.SetFocus
Exit Sub
End If
If (Me.txtLastName) = "" Then
MsgBox "Invalid Name"
Me.txtLastName.SetFocus
Exit Sub
End If
mclsStud.LastName = Me.txtLastName
mclsStud.StudentID = Me.txtID
mclsStudents.Add mclsStud
Me.txtID = ""
Me.txtLastName = ""
Me.txtID.SetFocus
End Sub
The problem is when I enter a second set of values in the text boxes, no matter what they are and no matter what the first ones are I get an error that says "This key is already associated with an element of this collection". I am at a complete loss...
Collections
Hi,
I've had trouble finding info on collections on the net, so here's my question:
I'm making an app that holds customer records in a collection of classes. I want 1 record to display at a time on my form, with prev and next buttons to skip through records.
How can I refer to one record at a time, and move up/down through the collection? The only way I can think is use a For/Each loop and find each record by using its key. This would be very inefficient, as I would have to scan through them each time the user clicks a nav button.
I'm basically looking for an alternative to an array, where I could simply put:
txtName.text = Customer(intCurrentRecord).Name
Thanks.
Collections
what is a collection/new collection and how are they used
Collections
Hi
What are the collections and what are the collection classes?
Thanx in advance.
Please Help Me With Collections
Hi, I hope you can help me. I've an assignment to do for my visual basic class in college, and I'm a little stuck. The question is called collections and says this:
Write a program to temporarily store and process student marks for a given exam. This system is designed to store exam marks for a group of students and then produce setails including:
the average mark for the group
the top mark for the group
the lowest mark for the group
You are to temporarily store the marks using a collection called StudentMarks. Initially prompt the user for the subject, number of students and available mark.
Then store the mark achieved against the student name. Once all the entries have been stored, displayed all entries in a list box having produced the percentage mark for each student.
So far I've made the collection and I think I've got the storing the name against the mark bit right. But I'm totally stumped on getting the entries back into the listbox I've never done collections before. If you could point me in the right direction I would be most grateful So far I've managed this:
VB Code:
Option Explicit Dim StudentMarks As New CollectionDim Subject As StringDim NumberOfStudents As IntegerDim AvailableMarks As Integer Private Sub cmdAdd_Click() Dim i As Integer Dim Student As String Dim Mark As Integer For i = 1 To NumberOfStudents Student = InputBox("Please enter the name of the student") Mark = InputBox("Please enter the mark for this student") StudentMarks.Add Mark, Student MsgBox "Entry Added" Next i End Sub Private Sub cmdOK_Click() Subject = txtSubject.Text NumberOfStudents = txtNumberOfStudents.Text AvailableMarks = txtAvailableMarks.Text txtSubject.Enabled = False txtNumberOfStudents.Enabled = False txtAvailableMarks.Enabled = False cmdOK.Enabled = FalseEnd Sub
Thanks in advance if you can help me
- Robert
Vb Collections
If I have an ActiveX dll created in Visual Basic which has a method that returns either a collection, ADO Recordset or an array can these returns be used and accessed in Delphi?
Collections
I have an application that reads data from a table into a collection.
Collection is of around 40,000 records. It takes almost 2 min to loop through the collection once to search for a record using
'For Next' loop. Isn't there a way using collections to get to that specific record without looping all of it?
Cause this seems to be much slower than just using select statements in SQL, then what is the point of using collections?
lngCount = collection.count
For lngCount = 1 to TotCount
If collection.Item(lngCount).varA = intVarA then
' do processing here
End if
Next
Collections
I have a class in a DLL that represents a table in my database, and I'm trying to use collections to return a recordset from this, but I'm running into problems. Basically, what I want to do is have a function in this class that opens a recordset, loops through it, appending all the records to a collection, and then returns the collection. Problem is, I can't figure out how to append the records to the collection.
What I have right now is something like this:
VB Code:
Public Function getRecords() As clsOrders Dim objCollection As New clsOrders rs.Open "select * from table1", cn, adOpenForwardOnly, adLockReadOnly Do While Not (rs.EOF) 'Add each record to some kind of object here?? objCollection.Add objSomeObject, CStr(rs.Fields("table1_id")) rs.MoveNext Loop Set getRecords = objCollectionEnd Function
Thanks for any help.
Collections And UDT's
In my program I have many UDT's that represent "controls" for the OpenGL UI my friend programmed (don't say VB rip off). btw...I'm writing the program that graphically allows you to place the controls. My problem is finding away to effectively store all the UDT's.
My first thought was a collection...that way I could simply use a For...Each loop to cycle through all the controls and generate the appropriate code. Two questions...can a Collection contain multiple UDT's (if it can even hold UDT's?!) and how I do determine what type of UDT the element.
However, if the above idea won't work I could write classes or make arrays...any ideas?
Collections
hey
where can i get a tutorial on collections and classes?
i learned classes awhile ago, but since i slacked on keeping up the skills, i forgot the initializations...
Collections
Hey everyone,
Is it possible to have a collection of your own Type For example
VB Code:
Public Type MyType strGUID as String strName as StringEnd Type Dim Foo as MyTypeDim Col as New collection For intC = 0 to 100 Foo.ID = intC Foo.Name = GetNameOf(intC) Col.Add Foo, RandomID()Next intC For each foo in Col 'Do StuffNext
But vb wont let me - is there anyway around this because im trying to remove all array usage from my code and replaceing them with collections, some arrays are 2 dimentaional and contain more than one bit of data - thus the type usage, so it would be real handy if i could do this - let me know - please
Thanks in advance
Collections
I was just wondering if anyone knew a good tutorial on creating and using collections. Or better yet, could tell me how to define one?!
Collections
I need to get all of the richtextboxes on my form:
VB Code:
Dim rText As RichTextBoxFor Each rText In frmNew.Controls Next rText
doesn't works, gives an "type mismatch" error
VB Code:
Dim rText As ControlFor Each rText In frmNew.Controls Next rText
does work, but then i get all of my controls
how do i get only my richtextboxes?
thanks
Collections?
Where can I find a good explaination of collections and objects? Tutorials, examples, and such?
Collections??
What are collections and how do you use them?? Do you use them instead of arrays, and redim ing them? I have herd of em. A url would help alot. Or if you would like to tell me about what is involved that would be great, thanks.
Collections
Can someone give an example on doing a collection? I know 0 about them.
Collections
Yes if it is public on modules declaration.
Thats same than any another variable
Collections Help Please!!!
What exactly does a collection do?
if i set a new collection in my form
will i be about to use the collection in other forms?
what is a collection of classes are there??
can only show me basics of a collection with examples
add , remove , items etc keywords?
understanding is more important to me than memorising the steps, i hope you guys can help
thanks alot
Collections
What is a collection and when would I use it?
Collections
'I have a collection object that contains a series of different names.
'If I wanted to change the order that they are stored in, for example swap name
' 1 with name 4, how would I do that.
' Is there a way to do it without adding a key?
Collections???????
Not a direct answer but it should give you the general idea of how to accomplish your task.
Code:
'Create a collection of controls with in a specific frame
'and access the controls within the new found collection
Option Explicit
'declare the name for the collection
Dim colFrame As Collection
Private Sub Command2_Click()
'add text to the textboxes with the collection colFrame
Call InFrameEvent
End Sub
Private Sub Form_Load()
'create a collection of controls within a frame
'declare object as control
Dim objCtrl As Control
'set the collection object as New Collection
Set colFrame = New Collection
'loop the collection of all controls
For Each objCtrl In Controls
'if the control is found inside Frame1 then add it to the
'collection we are building...colFrame
If objCtrl.Container Is Frame1 Then
colFrame.Add objCtrl
End If
Next
End Sub
Private Sub InFrameEvent()
'declare the object as control
Dim objCtrl As Control
'loop the collection (colFrame)
For Each objCtrl In colFrame1
'if the control is found in our collection (Frame1) then
'do something to objControl...add text to text box
If TypeOf objCtrl Is TextBox Then
objCtrl.Text = "I'm inside the frame"
End If
Next
End Sub
Collections
HI
I've got 11 Picturebox controls with the following indices:3,6,9,12,13,14,15,16,17,34,51.
I've tried to scroll through them for various operations using stuff like this:
Dim ctl as Control
For each ctl in Controls
If typeof Control is Picturebox then
'do stuff to them depending on their index
'for example:
Picture1(index).width = Index * 2
End if
Next
'Or this...
For i = 3 to 51
on error resume next
If Picture(i)=true 'index exists then
Picture(i).width = i *2
End if
Next
The first one doesn't work at all obviously and the second is pretty dirty and unefficient. What is the cleanest, most efficient way to do this for any collection(as well as mine)?
Thanks
JoeyO.
Collections
Can some please tell me how to use a collection i have created for my program? I want to know how to declare it, instantiate and call it. I used the VB Class Builder so it has all of the default property methods for accessing the variables.
cheers
Collections
I commonly use arrays when i need to store a lot of
data temporarily and access it easily.
In a program i'm writing now, i'm finding that i would
have to redim my array everytime i wanted to add
something to it and this is not feasible as i may need
to add 100+ or even 1000+ items at any given time
This is where collections come in. I don't know anything
about collections so what i'm asking is for someone to
please explain the basic functions of adding, removing
searching collection items and multidimensional
collections (if there is such a thing) and also (if possible)
the difference in storage and performance between a
collection and an array
thx for the help!
Collections
I'm trying working with a collection. Each item I add to a collection has three textboxes of info and text1 is added to a listbox. I'm trying to figure out how, when I click on a name in the list box to show all three textboxes of info for that item. I hope I made that clear enough. This is the first collection I've used.
Thanks,
JO
Collections
Hello, I am having trouble grasping the whole concept around collections. I was just wondering if i could get a different point of view or an example.. Thanks
Collections Help Please
adding collections.
i have added a collection to my project in a class module
but i get errors when trying to run it
what am i doing wrong help me please???
Code:
Private colDetails As New collection
'then using a function
colDetails.add newDetail, CStr(.id)
'and i get a runtime error '457'
'the key is already associated with an element of this
'collection any ideas what that means????
|