Below you will find a code sample that I have dummied up to try and demonstrate the issue I am having. However, this is just a dummied up piece of code. The actual program has noting to do with STUDENTS or TEACHERS. This was just my way of trying to reproduce just the basic code to demonstrate the issue I’m trying to solve.
Basically, the problem I’m having is trying to have a CLASS within another CLASS each of which contain ARRAYS that need to be dynamic in size. The sample has a CLASS called STUDENTS that can have in theory many students, the number of which are unknown to the CLASS. I need to be able to add STUDENTS to the ARRAY programmatically.
The CLASSROOM Class should also be an Array since each TEACHER will have MANY Students. There will be only 1 Teacher for a given classroom but the SCHOOL will have MANY CLASSROOMS. Again, I need to programmatically add Teachers and Students without having predefined Array sizes.
The code I have listed below will NOT work but it demonstrates what I am trying to achieve. I want to be able to reference the CLASS by using something like this ...
School(iTeacherIndex).Student(iStudentIndex) = "Rick Smith"
I feel like I should be able to use ARRAYLIST in both classes but haven’t been able to get it to work. I’ve tried DIM’ing as ARRAY and can’t get that to work either.
If I can get ARRAYLIST to work then I shouldn't need to worry about expanding the array via UBOUND (if it's even possible).
Needless to say I feel like I’m chasing my tail so now I’m asking for guidance.
As typically happens in a post like this people want to criticize the code without answering the question.
The REAL question is “How do I have a CLASS within a CLASS, each of which has ARRAYS that need to be dynamic in nature so they can be expanded as needed? The size of the array cannot be determined ahead of time”.
REMEMBER . . . the actual program has nothing to do with Students and Teachers. This was just my way of demonstrating the issue using an analogy that I thought everyone could understand. The actual Class is much more involved.
I have purposely hardcoded some values just to make the sample easier to work with. This is NOT how I normally program but is just here to hopefully make things a little more clear.
Thanks for any assistance you can give.
Here is the sample code . . .
'***********************************************************************************
'***********************************************************************************
'***********************************************************************************
'Here is the code that is in the form - This obviously doesn't work
Private Sub TeacherTestDemo()
Dim iTeacherIndex As Byte
Dim iStudentIndex As Byte
Dim School(0) As ClassRoom ' Need to be able to have MANY Classrooms
For iTeacherIndex = 1 To 2
iStudentIndex = 1
School(iTeacherIndex).TeachersName = "Mrs. Jones"
School(iTeacherIndex).Student(iStudentIndex) = "Rick Smith"
School(iTeacherIndex).Student(iStudentIndex) = "Jim Jones"
School(iTeacherIndex).Student(iStudentIndex) = "Jane Rogers"
iStudentIndex = 2
School(iTeacherIndex).TeachersName = "Mrs. Baker"
School(iTeacherIndex).Student(iStudentIndex) = "Cheryl Jefferson"
School(iTeacherIndex).Student(iStudentIndex) = "Brian Bennett"
School(iTeacherIndex).Student(iStudentIndex) = "Sandra White"
Next iTeacherIndex
End Sub
'***********************************************************************************
'***********************************************************************************
'***********************************************************************************
'Here is the code in the CLASS module (2 Classes - Students and ClassRoom)
Public Class Students
Private sStudents As New ArrayList
'-------------------------------------------------------------------------------
Public Property StudentName(ByVal iStudentIndex As Byte) As String
Get
Return sStudents(iStudentIndex)
End Get
Set(ByVal value As String)
sStudents.Insert(iStudentIndex, value)
End Set
End Property
'-------------------------------------------------------------------------------
End Class
'***********************************************************************************
'***********************************************************************************
'***********************************************************************************
Public Class ClassRoom
Private sTeachersName As String
Private sClassRoom(0) As Students
'-------------------------------------------------------------------------------
Public Property TeachersName() As String
Get
Return sTeachersName
End Get
Set(ByVal value As String)
sTeachersName = value
End Set
End Property
'-------------------------------------------------------------------------------
Public Property Student(ByVal iTeacherIndex As Byte, ByVal iStudentIndex As Byte) As String
Get
Return sClassRoom(iTeacherIndex).StudentName(iStudentIndex)
End Get
Set(ByVal value As String)
sClassRoom(iTeacherIndex).StudentName(iStudentIndex) = value
End Set
End Property
End Class