Creating Class properties with sub levels - excel

I've been reading this topic on how to use class modules.
My goal is to improve my code performance and readability so I think I'm in the right path.
But I have some questions about the limitations.
In my head i want to do this:
Is it possible to achieve such a structure?
The topic I've read has very few examples and this is not handled. I'm assuming this would be possible with collections of collections, but I not sure how to look for this.
My data comes from 2 tables, one has all the items but the department and the other one has the ID's alongisde the departments. Both tables have the dates of the current month as headers and their Schedule/Department depending on the table.
I'd know how to achieve this for one day, but not for a whole month.
This is how I wrote the basics for my class:
Option Explicit
Private DirNeg As String
Private Agrup As String
Private DNI As String
Private Centro As String
Private Servicio As String
Private Nombre As String
Property Get Business() As String
Business = DirNeg
End Property
Property Let Business(ByVal sBusiness As String)
DirNeg = sBusiness
End Property
Property Get Group() As String
Group = Agrup
End Property
Property Let Group(ByVal sGroup As String)
Agrup = sGroup
End Property
Property Get ID() As String
ID = DNI
End Property
Property Let ID(ByVal sID As String)
DNI = sID
End Property
Property Get Location() As String
Location = Centro
End Property
Property Let Location(ByVal sLocation As String)
Centro = sLocation
End Property
Property Get Service() As String
Service = Servicio
End Property
Property Let Service(ByVal sService As String)
Servicio = sService
End Property
Property Get Name() As String
Name = Nombre
End Property
Property Let Name(ByVal sName As String)
Nombre = sName
End Property
On the other hand, is it correct to fill the whole class on the Class_Initializeevent? My data will always be the same so I don't need to loop in a normal module to fill the class, it could be done everytime the class is created.
EDIT/UPDATE:
This is how my data looks like:
Schedules alongside Agent's info
Departments alongside Agent's ID
clAgent Class Module:
Option Explicit
Private DirNeg As String
Private Agrup As String
Private DNI As String
Private Centro As String
Private Servicio As String
Private Nombre As String
Private Fechas As Object
Property Get Business() As String
Business = DirNeg
End Property
Property Let Business(ByVal sBusiness As String)
DirNeg = sBusiness
End Property
Property Get Group() As String
Group = Agrup
End Property
Property Let Group(ByVal sGroup As String)
Agrup = sGroup
End Property
Property Get ID() As String
ID = DNI
End Property
Property Let ID(ByVal sID As String)
DNI = sID
End Property
Property Get Location() As String
Location = Centro
End Property
Property Let Location(ByVal sLocation As String)
Centro = sLocation
End Property
Property Get Service() As String
Service = Servicio
End Property
Property Let Service(ByVal sService As String)
Servicio = sService
End Property
Property Get Name() As String
Name = Nombre
End Property
Property Let Name(ByVal sName As String)
Nombre = sName
End Property
Property Get clFechas(ByVal StringKey As String) As clFechas
With Fechas
If Not .Exists(StringKey) Then
Dim objFechas As New clFechas
.Add StringKey, objFechas
End If
End With
End Property
Private Sub Class_Initialize()
Set Fechas = CreateObject("Scripting.Dictionary")
End Sub
clFechas Class Module:
Option Explicit
Private Modos As Object
Private Horarios As Object
'Aqiço creamos la propiedad Modo para la clase Fecha
Public Property Get Modo(ByVal StringKey As String) As String
Modo = Modos(StringKey)
End Property
Public Property Let Modo(ByVal StringKey As String, ByVal StringValue As String)
Modos(StringKey) = StringValue
End Property
Public Property Get Keys() As Variant
Keys = Modos.Keys
End Property
'Aquí creamos la propiedad Horario para la clase Fecha
Public Property Get Horario(ByVal StringKey As String) As String
Modo = Horarios(StringKey)
End Property
Public Property Let Horario(ByVal StringKey As String, ByVal StringValue As String)
Horarios(StringKey) = StringValue
End Property
Public Property Get Keys() As Variant
Keys = Horarios.Keys
End Property
'Iniciamos la clase
Private Sub Class_Initialize()
Set Modos = CreateObject("Scripting.Dictionary")
Set Horarios = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set Modos = Nothing
Set Horarios = Nothing
End Sub

You don’t seem to have any issues with regular properties so let’s focus on the complex ones; Schedule and Department. Both are the same, so same rules apply to both.
The property is basically list, the date is the index and the item is an object. I personally prefer to work with dictionaries as I can look if a key exist etc.
So, your Agent class could look something like this:
Option Explicit
Private m_schedules As Object
Public Property Get Schedule(ByVal Key As Date) As Schedules
With m_schedules
If Not .Exists(Key) Then .Add Key, New Schedules
End With
Set Schedule = m_schedules(Key)
End Property
'For testing purposes - can be ommited.
Public Property Get Keys() As Variant
Keys = m_schedules.Keys
End Property
'For testing purposes - can be ommited.
Public Property Get Count() As Long
Count = m_schedules.Count
End Property
Private Sub Class_Initialize()
Set m_schedules = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set m_schedules = Nothing
End Sub
The Schedules class:
Option Explicit
Private m_schedule As String
Public Property Get Schedule() As String
Schedule = m_schedule
End Property
Public Property Let Schedule(ByVal param As String)
m_schedule = param
End Property
Now, let's test it:
Sub Test()
Dim obj As Agent
Set obj = New Agent
obj.Schedule(#1/9/2019#).Schedule = "Schedule 1"
obj.Schedule(#2/9/2019#).Schedule = "Schedule 2"
obj.Schedule(#3/9/2019#).Schedule = "Schedule 3"
PrintToDebug obj
'Lets make a change
obj.Schedule(#2/9/2019#).Schedule = "Schedule 2222"
PrintToDebug obj
End Sub
Private Sub PrintToDebug(ByVal obj As Agent)
Debug.Print ""
Dim m As Variant
With obj
For Each m In .Keys
Debug.Print "Key: " & m & String(3, " ") & "Value: " & .Schedule(m).Schedule
Next m
End With
Debug.Print "Total Items: " & obj.Count
End Sub
Output:
'Key: 09/01/2019 Value: Schedule 1
'Key: 09/02/2019 Value: Schedule 2
'Key: 09/03/2019 Value: Schedule 3
'Total Items: 3
'Key: 09/01/2019 Value: Schedule 1
'Key: 09/02/2019 Value: Schedule 2222
'Key: 09/03/2019 Value: Schedule 3
'Total Items: 3
Additional information regarding the Dictionary object can be found here: Dictionary object
Also keep this in mind. It's quite important:
If key is not found when changing an item, a new key is created with
the specified newitem. If key is not found when attempting to return
an existing item, a new key is created and its corresponding item is
left empty.
If the dictionary item is not a simple string, let me know to update the answer. Sorry, I couldnt read the data in the screenshots. :)

Related

Managing Collections inside a Class

i'm trying to work with classes in vba and i haven't found any documentation related to managing objects inside objects such as arrays or collections inside a class.
Let's say i have a class person and a class address and i want to manage addresses of a person.
Address
Private pStreet as String
Private pZip as Int
Public Property Let Street(val As String)
pStreet = val
End Property
Public Property Get Street() As String
Street = pStreet
End Property
Public Property Let Zip(val As String)
pZip = val
End Property
Public Property Get Zip() As String
Zip = pZip
End Property
Person
Private pName As String
Private pSurname As String
Private pAddresses As Collection
Public Property Let Name(val As String)
pName = val
End Property
Public Property Get Name() As String
Name = pName
End Property
Public Property Let Surname(val As String)
pSurname = val
End Property
Public Property Get Surname() As String
Surame = pSurname
End Property
Private Sub Class_Initialize()
Set pAddresses = New Collection
End Sub
Private Sub Class_Terminate()
Set pAddresses = Nothing
End Sub
Public Sub addAddress(ByVal val As Address)
pAddresses.Add val
End Sub
Public Property Get Addresses() As Collection
Set Addresses = pAddresses
End Property
Public Property Get Address(ByVal Index As Long) As Address
Set Address = pAddresses(Index)
End Property
Module1
Sub test()
Dim x As Person
Set x = New Person
Dim a1 As Address
Set a1 = New Address
Dim a2 As Address
Set a2 = New Address
x.Name = "Mark"
x.Surname = "Doe"
a1.Street = "first avenue 213"
a1.Zip = "41242"
a2.Street = "second avenue 213"
a2.Zip = "55242"
x.addAddress a1
x.addAddress a2
Debug.Print x.Address(0)
End Sub
how should i deal with the address collection inside the person class?
for instance, how could i retrieve all the addresses or the second address of the collection? x.addresses(1) doesn't work.
The Person class should hold a collection of addresses, to avoid having to expand and preserve an array. The collection should be initialized/terminated as soon as the class is initialized/terminated.
See an example:
Private pAddresses as Collection
Private Sub Class_Initialize()
Set pAddresses = New Collection
End Sub
Private Sub Class_Terminate()
Set pAddresses = Nothing
End Sub
The class can expose the whole address collection via a property for looping, or a single address accessed by index.
Public Property Get Addresses() As Collection
Set Addresses = pAddresses
End property
Public Property Get Address(ByVal Index As Long) As Address
Set Address = pAddresses(Index)
End property
Then you can loop:
Dim p As Person, a As Address
For Each a In p.Addresses
a.Zip = ...
Next
Or get a single address by index:
Set a = p.Addresses(1)
Lastly, a simple Add method, to add an address to the person:
Public Sub AddAddress(ByVal param As Address)
pAddresses.Add param
End Sub
You could also add (or replace) an address using a property by supplying both the address object and the index, but I dont know how useful it is in your case. Of course you need to make sure Index is valid.
Public Property Let Address(ByVal Index As Long, ByVal param As Address)
pAddresses.Add param, Index
End property
Then to call it:
p.Addresses(Index) = a
To enforce the Set keyword, since we're dealing with objects, change Let to Set. Then, you need to set-it:
Set p.Addresses(Index) = a

Implement scripting.dictionary item

I'm trying to create a class that inherits Scripting.Dictionnary to create hash tables with type restrictive keys and items.
The problem I encounter is that I don't find any documentation about how to implement this, and I have an error message telling me I must implement Item to interface dictionary.
Here is the prototype of my class :
Option Explicit
Implements Dictionary
Public Sub Add(nom As String, jour As Date, temps As Integer)
Supplier.Add nom, Array(jour, temps)
End Sub
Public Property Get Item(Key As String) As Array
Item = Supplier.Item(Key)
End Property
Public Property Set Item(Key As String, jour As Date, temps As Integer)
Set Supplier.Item(Key) = Array(jour, temps)
End Property
How should I Implement Item to make it work ? And is this the good way to achieve what I want ?
Your stated goal is to implement a strongly-typed Dictionary. To accomplish this goal, I would not implement an Interface. Rather, I would wrap the Dictionary in a class and achieve the strong-typing by using another class:
Supplier Class
Option Explicit
Private Supplier As Dictionary
Private Sub Class_Initialize()
Set Supplier = New Dictionary
End Sub
Public Sub Add(Key As String, Item As SupplierItem)
Supplier.Add Key, Item
End Sub
Public Property Get Item(Key As String) As SupplierItem
Set Item = Supplier.Item(Key)
End Property
Public Property Set Item(Key As String, Value As SupplierItem)
Set Supplier.Item(Key) = Value
End Property
SupplierItem Class
Option Explicit
Public jour As Date
Public temps As Integer
Testing Logic
Option Explicit
Public Sub Test()
Dim s As Supplier
Dim si As SupplierItem
Set s = New Supplier
Set si = New SupplierItem
si.jour = Now
si.temps = 3
s.Add "Key1", si
Debug.Print s.Item("Key1").temps
Set si = New SupplierItem
si.jour = Now
si.temps = 4
Set s.Item("Key1") = si
Debug.Print s.Item("Key1").temps
End Sub
You will need to implement all the functions/properties of what you are implementing.
Something like so
Option Explicit
Private d As Scripting.Dictionary
Implements Scripting.Dictionary
Public Sub Class_Initialize()
Set d = New Scripting.Dictionary
End Sub
Public Property Set Dictionary_Item(Key As Variant, RHS As Variant)
Set d.Item(Key) = RHS
End Property
Public Property Let Dictionary_Item(Key As Variant, RHS As Variant)
d.Item(Key) = RHS
End Property
Public Property Get Dictionary_Item(Key As Variant) As Variant
End Property
Public Sub Dictionary_Add(Key As Variant, Item As Variant)
End Sub
Public Property Let Dictionary_CompareMode(ByVal RHS As Scripting.CompareMethod)
End Property
Public Property Get Dictionary_CompareMode() As Scripting.CompareMethod
End Property
Public Property Get Dictionary_Count() As Long
End Property
Public Function Dictionary_Exists(Key As Variant) As Boolean
End Function
Public Property Get Dictionary_HashVal(Key As Variant) As Variant
End Property
Public Function Dictionary_Items() As Variant
End Function
Public Property Let Dictionary_Key(Key As Variant, RHS As Variant)
End Property
Public Function Dictionary_Keys() As Variant
End Function
Public Sub Dictionary_Remove(Key As Variant)
End Sub
Public Sub Dictionary_RemoveAll()
End Sub

Access a a nested class from inside another nested class

I have one main class with 3 nested classes looking like this:
Main class:
Option Explicit
Private m_Login As Object
Private m_Archivo As Object
Private m_Equivalencia As Object
Private Property Get Logins(ByVal Key As String) As Logins
With m_Login
If Not .Exists(Key) Then .Add Key, New Logins
End With
Set Logins = m_Login(Key)
End Property
Private Sub Class_Initialize()
Set m_Login = CreateObject("Scripting.Dictionary")
Set m_Archivo = CreateObject("Scripting.Dictionary")
Set m_Equivalencia = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set m_Login = Nothing
Set m_Archivo = Nothing
Set m_Equivalencia = Nothing
End Sub
Private Property Get Keys() As Variant
Keys = m_Login.Keys
End Property
Private Property Get Count() As Long
Count = m_Login.Count
End Property
Private Property Get Items() As Variant
Items = m_Archivo.Keys
End Property
Public Property Get Archivos(ByVal Key As String) As Archivos
With m_Archivo
If Not .Exists(Key) Then .Add Key, New Archivos
End With
Set Archivos = m_Archivo(Key)
End Property
Public Property Get Equivalencias(ByVal Key As String) As Equivalencias
With m_Equivalencia
If Not .Exists(Key) Then .Add Key, New Equivalencias
End With
Set Equivalencias = m_Equivalencia(Key)
End Property
The three nested classes are, as you can see, Logins, Archivos and Equivalencias.
Now I'm trying to get the data stored in Equivalencias class from inside the Logins class.
When doing it from the main class I just need to write Equivalencias(id).Property for example, but I can't seem to call it from inside the nested class Logins.
How would I do it?
I need to do so because inside the Logins class I have this:
Public Property Get ModificacionCentro() As Boolean
If Not Baja And Not Alta Then
Select Case m_Site
Case "GLORIAS" And m_Centro <> "Barcelona Glorias (AESP) (GRAN VÍA DE LAS CORTS CATALANES 866-872, BARCELONA)"
ModificacionCentro = True
Case "ILUSTRACIÓN" And m_Centro <> "Madrid Ilustración (AESP) (C/SANTIAGO DE COMPOSTELA, 94, MADRID)"
ModificacionCentro = True
Case "TÁNGER" And m_Centro <> "MARRUECOS TANGER SUCURSAL (AESP) (RUE IBN FOURMAT ANGLE CARNOT, TANGER 90000 (MAROC))"
ModificacionCentro = True
Case Else
ModificacionCentro = False
End Select
End If
End Property
Right now I'm doing it hardcoded, but If I could access the other class I wouldn't need to, The code would be:
Public Property Get ModificacionCentro() As Boolean
If Not Baja And Not Alta Then
If Not Equivalencias(m_Site) = m_Centro Then ModificacionCentro = False
End If
End Property
And I wouldn't need to hardcode everytime it the sites would change.

Class module structure, get same property on different levels

I'm trying to build a class module with my whole Company as follows:
Now I know how to achieve this thanks to this answer.
The thing is I am on a planning area which will schedule everyone's work and shifts, so I need to add both Dates and timetable... I want to build a timetable for the whole day dividied by every single minute, so 1440 minutes.
If I were to write the 2 classes with the properties I need such as Auxiliar Time and Workers on the shift, could I call them from any other class without messing with each other?
My goal is to be able to for example sum the Aux time given a day, a group of minutes depending on the department, or the department in a specific location.
I understand this is going to be tedious to write, but is it possible to achieve such a thing?
My code right now calls the Agent which will have different properties, such as his location, service, name and area. By giving his ID you can fill these and if you feed the class a Date, you can get his schedule, department and a comment.
I understand this would have to change, all of it, but I just need to know if is possible what I'm trying to accomplish here, or is there any way more efficient than my initial structure.
My code goes as follows:
Company Class:
Option Explicit
Private ID As Object
Property Get clAgente(ByVal Key As String) As clAgente
With ID
If Not .Exists(Key) Then .Add Key, New clAgente
End With
Set clAgente = ID(Key)
End Property
Public Property Get Count() As Long
Count = ID.Count
End Property
Public Property Get Keys() As Variant
Keys = ID.Keys
End Property
Private Sub Class_Initialize()
Set ID = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set ID = Nothing
End Sub
Agent Class:
Option Explicit
Private Centro As String
Private Servicio As String
Private Nombre As String
Private DirNeg As String
Private Agrup As String
Private Fechas As Object
Property Get Business() As String
Business = DirNeg
End Property
Property Let Business(ByVal param As String)
DirNeg = param
End Property
Property Get Group() As String
Group = Agrup
End Property
Property Let Group(ByVal param As String)
Agrup = param
End Property
Property Get Location() As String
Location = Centro
End Property
Property Let Location(ByVal param As String)
Centro = param
End Property
Property Get Service() As String
Service = Servicio
End Property
Property Let Service(ByVal param As String)
Servicio = param
End Property
Property Get Name() As String
Name = Nombre
End Property
Property Let Name(ByVal param As String)
Nombre = param
End Property
Property Get clHorarios(ByVal Key As Date) As clHorarios
With Fechas
If Not .Exists(Key) Then .Add Key, New clHorarios
End With
Set clHorarios = Fechas(Key)
End Property
Private Sub Class_Initialize()
Set Fechas = CreateObject("Scripting.Dictionary")
End Sub
Private Sub Class_Terminate()
Set Fechas = Nothing
End Sub
Public Property Get Count() As Long
Count = Fechas.Count
End Property
Public Property Get Keys() As Variant
Keys = Fechas.Keys
End Property
And finally,
Date class:
Option Explicit
Private m_Horario As String
Private m_Modo As String
Private m_Coment As String
'Aquí creamos la propiedad Horario para la clase Fecha
Public Property Get Horario() As String
Horario = m_Horario
End Property
Public Property Let Horario(ByVal param As String)
m_Horario = param
End Property
'Aquí creamos la propiedad Modo para la clase Fecha
Public Property Get Modo() As String
Modo = m_Modo
End Property
Public Property Let Modo(ByVal param As String)
m_Modo = param
End Property
'Aquí creamos la propiedad Coment para la clase Fecha
Public Property Get Comentario() As String
Comentario = m_Coment
End Property
Public Property Let Comentario(ByVal param As String)
m_Coment = param
End Property
Any insight on the matter would be greatly appreciated.
In a word: Yes
[...] could I call them from any other class without messing with each
other?
Keep in mind that the class is just the structure and meaningless until instantiated and assigned to an object variable that can be copied and modified by any line of code within scope. So, whether or not any class has access to that variable is dependent on scope and whether or not you end up with a mess depends on you and our design.
[...] is it possible to achieve such a thing?
If you did not already know that it is possible to do this thing, then you will have a significant amount of learning to do before you can do this thing.
Any insight on the matter would be greatly appreciated.
Nothing you haven't heard before and you're not already doing: read, try, and learn.

Excel VBA: Is there a way to reference an instance of a class stored in a dictionary?

I currently have instances of classes stored using the data structure presented in the image below. Each -List item is a dictionary and each -Info item is an instance of a class.
I read elsewhere that if you Set an instance variable equal to another instance, it just references the original instance. Is this correct?
I have been able to create a reference for fileInfo(1) (in the image) using the following code.
Dim prflInfo As File_Info
Set prflInfo = New File_Info
Set prflInfo = fileList.Items(0)
I have attempted to reference the branchInfo instance using the following code, but I get a Run-time error 13: Type mismatch when I attempt to do so.
Dim prbrInfo As Branch_Info
With prflInfo
Set prbrInfo = New Branch_Info
brKey = .getbrKey(0)
Set prbrInfo = .getbrItem(brKey)
End With
Edit: Included below is the code for the File_Info class. All other classes follow this basic model.
'Class Module: File_Info
'Initialise class variables
Private pfileID As Integer
Private pfilePath As String
Private pfileName As String
Private pbranchList As Scripting.Dictionary
'Declare variantcopy subroutine
Private Declare Sub VariantCopy Lib "OleAut32" (pvarDest As Any, pvargSrc As Any)
Private Sub Class_Initialize()
Set pbranchList = New Scripting.Dictionary
End Sub
Public Property Let fileID(pfileIDi As Variant)
pfileID = pfileIDi
End Property
Public Property Get fileID() As Variant
fileID = pfileID
End Property
Public Property Let filePath(pfilePathi As Variant)
pfilePath = pfilePathi
End Property
Public Property Get filePath() As Variant
filePath = pfilePath
End Property
Public Property Let fileName(pfileNamei As Variant)
pfileName = pfileNamei
End Property
Public Property Get fileName() As Variant
fileName = pfileName
End Property
Public Sub addbrConn(branch As Branch_Info)
pbranchList.Add branch.branchID, branch.brConn
Debug.Print "addbrConn ID: " & branch.branchID
End Sub
Public Sub addBranch(branch As Branch_Info)
pbranchList.Add branch.branchID, branch
Debug.Print pbranchList.Count
End Sub
Public Function countbrList()
countbrList = pbranchList.Count
End Function
Public Function getbrKey(Key As Variant)
getbrKey = pbranchList.Keys(Key)
End Function
Public Function getbrItem(Key As Variant)
getbrItem = GetByRefVariant(pbranchList.Items(Key))
End Function
Public Sub dpbrList()
With pbranchList
Debug.Print pbranchList.Count
For k = 1 To pbranchList.Count
Debug.Print .Keys(k - 1), .Items(k - 1)
Next k
End With
End Sub
Public Sub updbrList(branch As Branch_Info)
Dim branchID As String
branchID = branch.branchID
If pbranchList.exists(branchID) Then
pbranchList.Remove (branchID)
pbranchList.Add branchID, branch
Debug.Print "Complete: " & branchID & " added."
Else
Debug.Print "Error: " & branchID & "does not exist."
End If
End Sub
Private Function GetByRefVariant(ByRef var As Variant) As Variant
VariantCopy GetByRefVariant, var
End Function
Is there a way to reference the branchInfo class, to make it easier to extract the data within it?
Thanks!
Eeshwar
I do things differently in that I iterate through the keys list using a For ... each loop rather than referring to an item number. Here is a snippet that works using two levels.
You can ignore the lines where the property values are written to an array, but they were part of the original code.
cf.Dependents is a dictionary of cDependents within the cFamily object
'Declarations in Main Module
Dim dF As Dictionary, cF As cFamily, cD As cDependents
Dim I As Long, J As Long
Dim V As Variant, W As Variant
...
For Each V In dF
I = I + 1
Set cF = dF(V)
With cF
vRes(I, 1) = .FirstName
vRes(I, 2) = .LastName
vRes(I, 3) = .Birthdate
J = 2
For Each W In .Dependents
J = J + 2
Set cD = .Dependents(W)
With cD
vRes(I, J) = .Relation
vRes(I, J + 1) = .DepName
End With
Next W
End With
Next V
Note that in the sequence, as you show in your question:
set Obj = new Obj
set Obj = myClass(0)
the first line is unnecessary.
IMO it is possible to use simple VBA.Collection, here example for the FileList and BranchList. In this example List classes have Items and Info classes have reference to List where List is wrapper for a VBA.Collection. HTH
For more reading have a look e.g. here.
FileList Class
Option Explicit
Private m_fileInfoCollection As FileInfoCollection
Private Sub Class_Initialize()
Set m_fileInfoCollection = New FileInfoCollection
End Sub
Public Property Get Items() As FileInfoCollection
Set Items = m_fileInfoCollection
End Property
FileInfo Class
Option Explicit
Private m_branchList As BranchList
Private m_fileID As Integer
Private Sub Class_Initialize()
Set m_branchList = New BranchList
End Sub
Public Property Get FileID() As Integer
FileID = m_fileID
End Property
Public Property Let FileID(ByVal vNewValue As Integer)
m_fileID = vNewValue
End Property
Public Property Get BranchList() As BranchList
Set BranchList = m_branchList
End Property
FileInfoCollection Class
Option Explicit
Private m_collection As VBA.Collection
Private Sub Class_Initialize()
Set m_collection = New VBA.Collection
End Sub
Public Sub Add(ByVal newItem As FileInfo)
m_collection.Add newItem, CStr(newItem.FileID)
End Sub
Public Function ItemByKey(ByVal key As String) As FileInfo
Set ItemByKey = m_collection(key)
End Function
Public Function ItemByIndex(ByVal index As Long) As FileInfo
Set ItemByIndex = m_collection(index)
End Function
Public Function Count() As Long
Count = m_collection.Count
End Function
BranchList Class
Option Explicit
Private m_branchInfoCollection As BranchInfoCollection
Private Sub Class_Initialize()
Set m_branchInfoCollection = New BranchInfoCollection
End Sub
Public Property Get Items() As BranchInfoCollection
Set Items = m_branchInfoCollection
End Property
BranchInfo Class
Option Explicit
Private m_branchID As Integer
Public Property Get branchID() As Integer
branchID = m_branchID
End Property
Public Property Let branchID(ByVal vNewValue As Integer)
m_branchID = vNewValue
End Property
BranchInfoCollection Class
Option Explicit
Private m_collection As VBA.Collection
Private Sub Class_Initialize()
Set m_collection = New VBA.Collection
End Sub
Public Sub Add(ByVal newItem As BranchInfo)
m_collection.Add newItem, CStr(newItem.branchID)
End Sub
Public Function ItemByKey(ByVal key As String) As BranchInfo
Set ItemByKey = m_collection(key)
End Function
Public Function ItemByIndex(ByVal index As Long) As BranchInfo
Set ItemByIndex = m_collection(index)
End Function
Public Function Count() As Long
Count = m_collection.Count
End Function
Standard Module
Option Explicit
Sub Demo()
' Fill
Dim bi As BranchInfo
Set bi = New BranchInfo
bi.branchID = 111
Dim fi As FileInfo
Set fi = New FileInfo
fi.FileID = 222
fi.BranchList.Items.Add bi
Dim fl As FileList
Set fl = New FileList
fl.Items.Add fi
' Get
Dim fi1 As FileInfo
Set fi1 = fl.Items.ItemByIndex(1)
Dim bi1 As BranchInfo
Set bi1 = fi1.BranchList.Items(1)
End Sub

Resources