store data for student names and student degrees.

Jean101

Member
Joined
Feb 8, 2020
Messages
10
Programming Experience
1-3
I want to be able to have multiple (student.degrees) it can be the same values, and for (student.name) to be different key at all time. My issue is whenever I tried adding another student.degree with the same degree name it throw errors.

C#:
private void AddStudent()
        {
            Console.Clear();
            //Setting UI color
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Add Student: ");
            Console.WriteLine("============");
            Console.WriteLine("============");
            Console.ForegroundColor = ConsoleColor.Gray;

            Student myStudent = new Student();

            myStudent.name = Validation.ValidateString("\nPlease enter student name: ");
            _school.Add(myStudent.name, new List<Student>());

            myStudent.degree = Validation.ValidateString("Please enter student degree: ");
            _school.Add(myStudent.degree, new List<Student>());

            _school[myStudent.name].Add(myStudent);
            //_school[myStudent.degree].Add(myStudent);

            //if (_school == null)
            //{
            //    _school = new Dictionary<string, List<Student>>();
            //}

            
            Console.WriteLine($"New student created {myStudent.name} {myStudent.degree}");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            _myMenu.Display();
            Selection();
        }

        private void ShowAllStudents()
        {
            Console.Clear();
            //Setting UI color
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Show All Students: ");
            Console.WriteLine("==================");
            Console.WriteLine("==================");
            Console.ForegroundColor = ConsoleColor.Gray;

            int counter = 0;
            Console.WriteLine($"{"  "} {"Name", -15} {"Degree", -10} {"Grades", -15} {""}");
            foreach (KeyValuePair<string, List<Student>> kvp in _school)
            {
                //Console.WriteLine($"{kvp.Key}");

                foreach (Student student in kvp.Value)
                {
                    counter++;
                    Console.WriteLine($"{counter}. {student.name, -15} {student.degree, -10}");
                }
            }
 
I want to be able to have multiple (student.degrees) it can be the same values, and for (student.name) to be different key at all time. My issue is whenever I tried adding another student.degree with the same degree name it throw errors.

C#:
private void AddStudent()
        {
            Console.Clear();
            //Setting UI color
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Add Student: ");
            Console.WriteLine("============");
            Console.WriteLine("============");
            Console.ForegroundColor = ConsoleColor.Gray;

            Student myStudent = new Student();

            myStudent.name = Validation.ValidateString("\nPlease enter student name: ");
            _school.Add(myStudent.name, new List<Student>());

            myStudent.degree = Validation.ValidateString("Please enter student degree: ");
            _school.Add(myStudent.degree, new List<Student>());

            _school[myStudent.name].Add(myStudent);
            //_school[myStudent.degree].Add(myStudent);

            //if (_school == null)
            //{
            //    _school = new Dictionary<string, List<Student>>();
            //}

           
            Console.WriteLine($"New student created {myStudent.name} {myStudent.degree}");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            _myMenu.Display();
            Selection();
        }

        private void ShowAllStudents()
        {
            Console.Clear();
            //Setting UI color
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Show All Students: ");
            Console.WriteLine("==================");
            Console.WriteLine("==================");
            Console.ForegroundColor = ConsoleColor.Gray;

            int counter = 0;
            Console.WriteLine($"{"  "} {"Name", -15} {"Degree", -10} {"Grades", -15} {""}");
            foreach (KeyValuePair<string, List<Student>> kvp in _school)
            {
                //Console.WriteLine($"{kvp.Key}");

                foreach (Student student in kvp.Value)
                {
                    counter++;
                    Console.WriteLine($"{counter}. {student.name, -15} {student.degree, -10}");
                }
            }



I want to have multiple web and multiple mobile (for student.degree)
Screen Shot 2020-02-09 at 5.56.47 AM.png
 
If your Dictionary is supposed to store names as keys and degrees as values, why are you storing both names and degrees as keys and then Lists of Student objects as values? There's not a lot of sense to that. If all you have is one String containing the name of a student and one String containing the name of a degree, your Student type is useless. Create a Dictionary<string, string> and then, when you add an item, add the name of the student and the name of the degree together, e.g.
C#:
private Dictionary<string, string> degreesByStudent = new Dictionary<string, string>();
C#:
Console.WriteLine("Enter student name:");

var studentName = Console.ReadLine();

Console.WriteLine("Enter degree name:");

var degreeName = Console.ReadLine();

degreesByStudent.Add(studentName, degreeName);
 
If you stick to OOR (Object orientation rules), just as you appeared to do with your student creation, you would be on the right track.

Also, what happens if your student shares the exact same name as another pupil? Given that dictionaries require a unique key, you can only expect an error if your key value provided is being provided more than once. Dictionary<TKey, TValue>
Every key in a Dictionary<TKey,TValue> must be unique according to the dictionary's equality comparer.
 
Back
Top Bottom