surpavan
surpavan

Reputation: 1422

Classes, references to save memory

I have a class as follows:

Public Class Courses
    Public CoursesOfferedMAIN As New List(Of Category)
    Public CoursesList As New List(Of Course)
    Public SemsList As New List(Of Sem)
    Public SubjectsList As New List(Of Subjects)
    Public ExamsTypeList As New List(Of ExamType)

    Public Class Category
        Private CategoryName As String
        Private Deleted As Boolean
        Public Courses As New List(Of Course)
    End Class
    Public Class Course
        Private CategoryName As String
        Private CourseID As String
        Private CourseName As String
        Private Deleted As Boolean
        Public Sems As New List(Of Sem)

    End Class
    Public Class Sem
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SemName As String
        Private Deleted As Boolean
        Public Subjects As New List(Of Subjects)
    End Class
    Public Class Subjects
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SubjectID As String
        Private SubjectName As String
        Private Deleted As Boolean
        Public Exams As New List(Of ExamType)
    End Class
    Public Class ExamType
        Private CategoryName As String
        Private CourseID As String
        Private SemID As String
        Private SubjectID As String
        Private ExamTypeID As String
        Private ExamName As String
        Private ExamMax As String
        Private ExamMin As String
        Private ExamPass As String
        Private Deleted As Boolean
    End Class

    Public Sub UpdateLists()
        CoursesList.Clear()
        SemsList.Clear()
        SubjectsList.Clear()
        ExamsTypeList.Clear()
        For Each Cat As Category In CoursesOfferedMAIN
            For Each cour As Course In Cat.Courses
                CoursesList.Add(cour)
                For Each sems As Sem In cour.Sems
                    SemsList.Add(sems)
                    For Each subj As Subjects In sems.Subjects
                        SubjectsList.Add(subj)
                        For Each exam As ExamType In subj.Exams
                            ExamsTypeList.Add(exam)
                        Next
                    Next
                Next
            Next
        Next
    End Sub
End Class

There is no problem with the code how ever, I would like to know, in the process I am following, the number of subjects, couses, examsets etc would all get repeated in the specific type lists like SubjectsList, examstypelist etc.. since they are being copied over it would take more memory.

Hence, my question or doubt would be, is it possible to use the same as references instead of copying them over to lists to save memory, or are there any methods better than this?

Upvotes: 0

Views: 214

Answers (1)

Guffa
Guffa

Reputation: 700720

You are already using references. Classes are reference types, so when you copy the objects you are actually just copying the references to the objects.

Upvotes: 3

Related Questions