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
Public Class Student

	Public Property Name As String

End Class

Public Class Teacher

	Public Property Name As String

End Class

Public Class ClassRoom

	Public Property ThisTeacher As Teacher

	Public Property Students As List(Of Student)

	Public Sub New()
	End Sub

	Public Sub New(teacher As Teacher, students() As Student)
		Me.ThisTeacher = teacher
		Me.Students = New List(Of Student)(students)
	End Sub
End Class

Example:

Dim rooms As New List(Of ClassRoom)
                'method#1
		Dim room As New ClassRoom 
		With room
			.ThisTeacher = New Teacher With {.Name = "Mrs. Jones"}
			.Students = New List(Of Student)({New Student With {.Name = "Rick Smith"}, New Student With {.Name = "Rick Smith"}, New Student With {.Name = "Jane Rogers"}})
		End With
		rooms.Add(room)
                'method#2
		Dim room2 As New ClassRoom(New Teacher With {.Name = "Mrs. Baker"}, {New Student With {.Name = "Cheryl Jefferson"}, New Student With {.Name = "Brian Bennett"}, New Student With {.Name = "Sandra White"}})
		rooms.Add(room2)

		'testing
		For Each _room In rooms
			Debug.WriteLine("Teacher: {0} => Students: {1}", _room.ThisTeacher.Name, String.Join(", ",_room.Students.Select(Function(s) s.Name)))
		Next

		'Output:
		'Teacher: Mrs. Jones => Students: Rick Smith, Rick Smith, Jane Rogers
		'Teacher: Mrs. Baker => Students: Cheryl Jefferson, Brian Bennett, Sandra White

I understand most of what is being done in the answer to my post but as you could probably tell from my original post that I suffer sometimes from “Old School” programming habits.

Again, I want to emphasize that this application has nothing to do with Teachers or Students but to describe my issue we will continue with that metaphor.

I need to be able to Access, Add, Delete and Completely Remove anywhere from a single student to a whole classroom. I need to be able to do this via an INDEX. The array MUST be kept in the order in which I place it and I need to be able to ADD or DELETE items based on an INDEX value.

The “Really OLD School” way of handling this (we will call this Method 1) with just a simple String Array would be something like . . .

‘Code in the VB form . . .

 Sub Demo1()

        Dim Student(0) As String
        Dim sStudentName As String
        Dim iStudentIndex As Integer

        iStudentIndex = 1
        sStudentName = "Jane Doe"
        Call AddStudent(Student, iStudentIndex, sStudentName)

    End Sub


    Sub AddStudent(ByVal Student(), ByVal iStudentIndex, ByVal sStudentName)

        If iStudentIndex > Ubound(Student, 1) Then
            ReDim Preserve Student(iStudentIndex)
        End If

        Student(iStudentIndex) = sStudentName

    End Sub

The slightly “Newer School” method (we will call this Method 2) would be to use a Class as in ...

Sub Demo2()

        Dim Student As New Students
        Dim sStudentName As String
        Dim iStudentIndex As Integer

        iStudentIndex = 1
        sStudentName = "Jane Doe"
        Student.StudentName(iStudentIndex) = sStudentName

    End Sub


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

It’s at this point that I get a little lost.

With the code that was provided above I’m not sure how to Reference/Add/Delete a NEW Student or REPLACE a Teacher’s Name. Again, I want to emphasize that I need to be able to reference the Student and Teacher with an INDEX. If I had 5 students in the ARRAY and I wanted to DELETE the 3rd Student in the ARRAY I need to be able to reference the 3rd student with an INDEX of 3 and set the value to "" (I know, it's the old school way of saying "nothing"). The ARRAY element would still be there but it would be EMPTY as far as I’m concerned since the string length is 0. I also need to be able to ADD (Insert) say 5 Students in between Students 3 and 4, shoving the existing 4th and 5th Students DOWN so now I have 10 Students total with what used to be students 4 and 5 now at position 9 and 10.

Method 2 above accomplishes all of this without resorting to the OLD ReDim Preserve nonsense. I can use all of the Methods available to the ArrayList.

I need to be able to accomplish the same type of task with the Classroom. I need to be able to access the Classroom with and INDEX and be able to ADD or DELETE Classrooms in a manor similar to the example above for Students. That’s why in my Original code I was hoping to be able to access the Classroom with something like . . .

Retreiving the values should be as simple as . . .

TeachersName = Classroom(iClassroomIndex).TeachersName
StudentName = Classroom(iClassroomIndex).StudentName(iStudentIndex)

Setting the values gets a little more involved depending on what action you are wanting to perform such as Add, Insert, Remove, Clear, etc.

Classroom(iClassroomIndex).TeachersName.Insert = “Mrs. Smith”
Classroom(iClassroomIndex).StudentName(iStudentIndex).Insert = “John Doe”

I know my syntax above is not correct but this is what I’m trying to achive so that I can access the Students and/or Teachers with Index’s and ALL of the Arrays will dynamically expand to handle the data.

I’m hoping this is possible. I just can’t figure out how to use the code that I was provided above and make the references as I described.

Remember, these Arrays are NOT static at design time. As the user edits the data the arrays will expand as needed. If I want to Delete (Remove) a Student I will simply set the Array element to "". I don’t need to technically delete the element from the Array (I might if I can use the ArrayList methods but that’s not a requirement).

So, as you can see, my “Old School” thinking has me blocked on how to handle this.

Any help someone can offer would be greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.