How to link two classes and then output class using DB4O as Database and C# as language?

87 Views Asked by At

I am doing my homework about using DB4O as NoSQL, it pretty new to me, then I reached to link two classes. There is a Course class with these properties:

 public class Course
    {
        public string CourseCode { get; set; }
        public string CourseName { get; set; }

        public Course(string code, string name)
        {
            CourseCode = code;
            CourseName = name;
        }

        public Course(string code)
        {
            CourseCode = code;
        }
}

and a Student class with these properties:

public class Student
    {
        public string StudentCode { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public int AdYear { get; set; }
        public DateTime BirthDate { get; set; }
        public int Age { get; set; }
        public Course Course { get; set; }

        public Student(string studentCode, 
                    string lastName, 
                    string firstName, 
                    int adYear, 
                    DateTime birthDate,
                    int age,
                    Course course)
        {
            StudentCode = studentCode;
            LastName = lastName;
            FirstName = firstName;
            AdYear = adYear;
            BirthDate = birthDate;
            Age = age;
            Course = course;
        }

        public Student(string studentCode)
        {
            StudentCode = studentCode;
        }

    }

This is how I add a Student with a Course:

private void button_Add_Click(object sender, EventArgs e)
        {
            //If exist
            var course = new Course(comboBox_CourseCode.Text);

            var student = new Student(textBox_StudentCode.Text,
                                    textBox_LastName.Text,
                                    textBox_FirstName.Text,
                                    Int32.Parse(textBox_AdYear.Text),
                                    DateTime.Parse(dateTimePicker_BirthDate.Text),
                                    Int32.Parse(textBox_Age.Text),
                                    course);
            resultStudent = dbStudent.QueryByExample(student);
            dataGridView_Student.DataSource = resultStudent;

            if (dataGridView_Student.Rows.Count == 0)
            {
                dbStudent.Store(student);

                loadStudent();
            }

            else
            {
                if(textBox_LastName.Text == student.LastName && 
                    textBox_FirstName.Text == student.FirstName &&
                    comboBox_CourseCode.Text != student.Course.CourseCode)
                {
                    dbStudent.Store(student);
                }
                else
                {
                    MessageBox.Show("Already have a student in this course");
                }
                loadStudent();
            }

            //Clear
            textBox_StudentCode.Clear();
            textBox_LastName.Clear();
            textBox_FirstName.Clear();
            textBox_AdYear.Clear();
            textBox_Age.Clear();
        }

But the output that I want is the Course output as CourseCode and CourseName, but it outputs like this: Picture of Student form

So is there a way to make it output all Course properties just by link two classes? Thank you very much.

1

There are 1 best solutions below

1
Hazrelle On

Just override the ToString() method, and this should output what you want. Modify the format as you like.

public class Course {

  public override ToString() {
    return $"{CourseCode} - {CourseName}";
  }
}