PDA

View Full Version : Solved: Help with Objects in a Collection



mferrisi
06-25-2010, 10:33 AM
Hi,

I have created a bunch of objects and indexed them in a collection. I am able to return the properties of the objects by

oEmployeeColl(4).Name but am unable to return object properties that are arrays. For clarifiction, I have a collection

Public oEmployeeColl As New Collection is the collection of employees.

That has a bunch of objects indexed on it
Sub Add_Employee_Info(EmployeeNum, ByRef Name As String, ByRef Type As String, ByVal DTime As Date, RTime As Date)
Dim oEmployee As Employee
Set oEmployee = New Employee
oEmployee.AddEmployee EmployeeNum, Name, Type, DTime, RTime
oEmployeeColl.Add oEmployee


The objects were created in a class module called Employee
Public EmployeeName As String
Private DispatchReturn()

Public Function AddEmployee(sEmployeeNum, ByVal Name As String, Type As String, DTime As Date, RTime As Date)
ReDim DispatchReturn(1 To 1, 1 To 4)
EmployeeName = Name
DispatchReturn(1, 1) = sEmployeeNum
DispatchReturn(1, 2) = Name
DispatchReturn(1, 3) = DTime
DispatchReturn(1, 4) = RTime
End Function

So now, oEmployeeColl(1).Employee name returns the name,
but I can't get oEmployee(1).DispatchReturn(1,3)

Bob Phillips
06-25-2010, 12:05 PM
Why do you have an array in the class, why not separate properties for each attribute?

mferrisi
06-25-2010, 12:14 PM
Hi,


Each Employee can have multiple arrivals and departures. So, the array might require a Redim Preserve as well if the employee departs and returns more than once.

Bob Phillips
06-25-2010, 03:04 PM
I would have separate arrays of each property, or another collection class within the employee collection class, but using your code, change the arry declaration to



Public DispatchReturn As Variant