Organization and C # Class Lists

I am working on a C # application which consists of Department, Course and Section objects. Each department has many courses, and each course has many sections. I currently have three classes: department, course, and section. The department contains some properties, followed by a list of courses that contains the courses offered by the department. The course contains some properties, and then sections of the list that contain sections of the course. Is this a good way to structure my code, or should I do it differently?

Secondly, when I create a department instance in my application, I set some properties and then I would like to start adding courses to the Courses list defined in the Department class. However, I can't seem to just make Department.Courses.Add (course) from the application. What should I do in the department class to add objects to this list without violating the encapsulation principle?

An example of what I have with the list:

class Department
{

     // ......
     List<Course> Courses = new List<Course>;
}

however, in the Code section, the program code is not available after the class has been created (all other class properties are available).

+3
source share
4 answers

Create an instance of the internal cursor list inside the constructor without parameters.

private List<Course> _coursesList;

public Department()
{
    _coursesList = new List<Course>();
}

, - , , . -

public void AddCourse(Course c) { ... }

// or (adding the feature of doing the method calls in a composable way)
public Course AddCourse(Course c) { ... }

// or 
public void AddCource(String name, etc) { ... }

, , List , Add Remove, . , , , API (, ), API .

public Course[] Courses {
    get { return _coursesList.ToArray(); }
}

, IEnumerable . LINQ, # 3.0.

, , .

+3

- . . .

public class Department
{
    // Initialize the list inside Default Constructor
    public Department()
    {            courses = new List<Course>();        }

    // Initialize List By Declaring outside and Passing with Dpartment Initilization
    public Department(List<Course> _courses)
    {            courses = _courses;        }

    List<Course> courses;
    public List<Course> Courses
    {
        get
        {
            if (courses == null)
                return new List<Course>();
            else return courses;
        }
        set { courses = value; }
    }
    internal bool AddCourseToCourses(Course _course)
    {
        bool isAdded = false;
        // DoSomeChecks here like
        if (!courses.Contains(_course))
        {
            courses.Add(_course);
            isAdded = true;
        }
        return isAdded;
    }
}    

public class Course
{
    public Course(List<Subject> _subject)
    {            subjects = _subject;        }

    List<Subject> subjects;  
    public List<Subject> Subjects
    {
        get { return subjects; }
        set { subjects = value; }
    }
}

// I do not get what do you mean by course "section", very general.
// used Subject instead, Change as you want just to give an idea
public class Subject
{
    string name;    
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    int creditHours;    
    public int CreditHours
    {
        get { return creditHours; }
        set { creditHours = value; }
    }
    public Subject(string _name, int _creditHours)
    {
        name = _name;
        creditHours = _creditHours;
    }
}
public class TestClass
{
    public void DoSomething()
    {  
        // Subjects
        Subject subj1 = new Subject("C#", 10);
        Subject subj2 = new Subject(".Net", 10);

        // List of Subjects
        List<Subject> advancedPrSubjects = new List<Subject>();
        advancedPrSubjects.Add(subj1);
        advancedPrSubjects.Add(subj2);

        // Course
        Course advancedProgramming = new Course(advancedPrSubjects);            


        // Deliver authoroty to add Course to Department Class itself
        Department dept = new Department();
        dept.AddCourseToCourses(advancedProgramming);
    }
}

.

+2

- - , , .

, ,

List<xxxx> _variable;

vs

List<xxxx> _variable = new List<xxxxx>();

( ());

0

, . . , , . , .

0

Source: https://habr.com/ru/post/1731017/


All Articles