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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection Class Key property

Status
Not open for further replies.

hdougl1

Programmer
Dec 6, 2000
54
US
When adding class properties to my collection with the .add method, I also give a key value. I what to maitain the key as a static position. e.g. If the collection has 5 objects and I delete the third one I need to know how to reference the last object as index value of 5 even though the collection has only 4 objects. Is this posible???

Thats in advanced!!!
Henry
 
If you only need to reference the Last item of your collection, then you can use the Count Property to address it:

With colMyCollection
varValue = .Item(.Count)
End With


REMEMBER: The collection object is ONE-BASED _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
The key needs to be alpha-numeric. so if your program is keeping track of an "Index", then set the key to something like "E" & Index (see below)
Dim colMyCollection As Collection
Dim intEmpID As Integer
Dim objMyObject As New OrgChart.Employee 'this is my class
Dim rsEmployees As ADODB.Recordset
Dim strNames As String

'This populates my collection with all employees
Set rsEmployees = GetEmployees()
With rsEmployees
Do Until .EOF
objMyObject.EmpID = .EmpID
objMyObject.ReportToID = .ReportToID 'this is the boss's EmpID
objMyObject.LastName = .LastName
'etc.
colMyCollection.Add objMyObject, "E" & CStr(.EmpID) 'I add a letter so the program knows it's a key, not an index#
.MoveNext
Loop
End With

'This finds the corporate hierarchy of an employee
'giving a string that list all of the employee's superiors
'The president of the company doesn't have a boss, so his .ReportToId = 0
intEmpID = CInt(Text1.Text)
Set objMyObject = colMyCollection("E" & CStr(intEmpID))
strNames = objMyObject.LastName
intEmpID = objMyObject.ReportToID

Do Until intEmpID = 0
Set objMyObject = colMyCollection("E" & CStr(intEmpID))
strNames = objMyObject.LastName & ", " & strNames
intEmpID = objMyObject.ReportToID
Loop

'the output might look like this(the president is Jones, the employee Smith:

'Jones, Davis, Doe, Smith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top