Home > database >  Trying to compare class fields C#
Trying to compare class fields C#

Time:12-29

Below I have the constructors of two classes. Student and Course

I am trying to compare the value of _completedCourses value from the Student class against the coursecode of the Course class.

_completedCourses is a dictionary of Courses and their grades. I am trying to find which course is not completed and print that list.

Am I going about this correct.

public Course(string coursName, string courseCode, char passingGrade, double noOfCredits, Semester semesterOfferd, int major)
        {
            this.CourseName = coursName;
            this.CourseCode = courseCode;
            this.PassingGrade = passingGrade;
            this.NoOfCredits = noOfCredits;
            this.SemesterOfferd = semesterOfferd;
            this.prerequisiteCourses = new List<Course>();
            this._enrolledStudents = new List<Student>();
            this._major = major;
        }

 public Student(int studentID, string studentName, Status studentStatus, StudentMajor studentMajor)
        {
            this._studentID = studentID;
            this._studentName = studentName;
            this._studentStatus = studentStatus;
            this._studentMajor = studentMajor;
            this._course = new Course();
            this._completedCourses =  new Dictionary<string, char>();
            countStudent  ;
        }

  public void RemainingCourses() 
        {
           var cKey = _completedCourses.ContainsKey(_course.CourseCode);
            Console.WriteLine("Remaining Courses");
            if (cKey.Equals(_course.CourseCode))
            {

                foreach (KeyValuePair<string, char> count in _completedCourses)
                {
                    {
                        {
                            Console.WriteLine(count.Key   " "   count.Value);
                            //          Console.WriteLine("Course "    count.Value);
                            count.ToString();
                        }
                    }
                }
            }
        }

UPDATE!!!

The following line of code in my driver class achieves what I want

Console.WriteLine("Enter Student ID ");
                    input = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    if (input.Equals(id.StudentID)) {
                        id.DisplayCompletedCourse();
                        foreach (var sub in isdCourses) {
                            var cKey = id.CompletedCourses.ContainsKey(sub.CourseCode);
                            if (!cKey)
                            { 
                                Console.WriteLine(sub.CourseCode);
                            }
                        }
                    }

CodePudding user response:

The ContainsKey method returns a boolean value (true/false), so in your if condition:

if (cKey.Equals(_course.CourseCode))

cKey is a boolean but _course.CourseCode is a string, so this will never be true, thus this will never go within the if block.

Try to rewrite it like this:

public void RemainingCourses() 
{
    var cKey = _completedCourses.ContainsKey(_course.CourseCode);
    Console.WriteLine("Remaining Courses");
    if (cKey) // if cKey is "true", then the dictionary contains the CourseCode
    {
        foreach (KeyValuePair<string, char> count in _completedCourses)
        {
            Console.WriteLine(count.Key   " "   count.Value);
        }
    }
}

As a side note, please beware of the formatting, you are adding unnecessary curly braces. Also, this could be simplified with linq, but I kept it as close as possible to the original code.

  • Related