First of all, excuse my explanation of my problem if I use incorrect terminology, I'm not experienced and self-taught.
I am learning to use class modules to set up "Objects" to easier reference variables and to run common functions. The issue I'm having is that I can't find information on how to set up a class module which can act as a collection to make use of the add function inherent in a collection.
For Example, I have my parent class module named clsSchool
in this class module, I have defined an object so that I can set a "child" class, clsTeacher
In my class module for clsTeacher, I have set a string variable Name. This is how my 2 class modules look.
clsSchool
Public Student As Object
clsTeacher
Public Name as String
In my module I have
modSchool
Set mySchool = New clsSchool
Set mySchool.Teacher = New clsTeacher
mySchool.Teacher.Name = "Jim"
At this point, my code is exactly what I want it is very easy to use the variable mySchool.Teacher.Name to recall "Jim", I can also use a while loop with mySchool.Teacher to recall various variables I have defined in my clsTeacher
I problem is that I want to add multiple teachers without having to set multiple classes at the top of my code as the number of teachers can vary. I.e. the following does work but has limitations.
modSchool
Set mySchool = New clsSchool
Set mySchool.Teacher1 = New clsTeacher
Set mySchool.Teacher2 = New clsTeacher
Set mySchool.Teacher3 = New clsTeacher
mySchool.Teacher1.Name = "Jim"
mySchool.Teacher2.Name = "Jack"
mySchool.Teacher3.Name = "John"
What I would like is something similar to how collections work. I.e. I want to find the total number of teachers (which varies) and create a for loop to create a new clsTeacher for each unique teacher. What I want is something like the following, but I'm stuck and I can't find any resources which help explain how to set up the following.
modSchool
Set mySchool.Teacher = New clsTeacher
n_teachers = 6
for i=1 to n_teachers
mySchool.Teacher(i).Name = Range("A1").Offset(i,0)
Next i
This way I can easily recall the name of teacher 1 or teacher 2 and use while/for loops to do so.