Dictionary List<>

Jean101

Member
Joined
Feb 8, 2020
Messages
10
Programming Experience
1-3
I'm having a hard time storing multiple names and degrees to my dictionary list.

Im using a menu so user can add multiple names and degrees but only one name and degree saving to my dictionary list, when I click on my menu Show All Students.


Screen Shot 2020-02-08 at 6.30.52 PM.png

Screen Shot 2020-02-08 at 6.31.04 PM.png
Screen Shot 2020-02-08 at 6.30.52 PM.png
 
First things first, please don't post pictures of code. Code is text. Post it as text and then format it as code using the toolbar. I want to show you where in your code the mistake is but now I can't just copy and paste the relevant code because it's in a picture. At least I only have to retype a bit of the code to do this. What if we wanted to be able to test your code for ourselves? We'd have to retype it all. You're not going to make many friends forcing us to do that.

Anyway, the issue is that, every time you add an item to the Dictionary, you create a new Dictionary first:
C#:
_school = new Dictionary<string, List<Student>>();

//...

_school.Add(myStudent.name, new List<Student>());
That means that, before you add each new student, you first throw away all existing students. You only want one Dictionary, right? So you should only be executing the code that creates a new Dictionary once. One option would be to test whether one exists before creating one, e.g.
C#:
if (_school == null)
{
    _school = new Dictionary<string, List<Student>>();
}
The most sensible thing to do, though would be to simply create the Dictionary where you declare the variable:
C#:
private Dictionary<string, List<Student>> _school = new Dictionary<string, List<Student>>();
 
Perhaps I'm missing something, by why are you adding the student name as a key with an empty student list on line where you have:
C#:
_school.Add(myStudent.name, new List<Student>());
 
Thank you for the head up about the picture. I just signup today I'm new to this site.

I want name to be the key and degree to be the value. For some reason when i type web for degree twice it crash.
 
Perhaps I'm missing something, by why are you adding the student name as a key with an empty student list on line where you have:
C#:
_school.Add(myStudent.name, new List<Student>());


Show All Students:


==================


==================


Name Degree Grades


1. jean web


2. max mobile


Press any key to continue...

That's my display menu. If i want to add a new student with the same degree as web it crash.
 
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;

            //Instantiation
           _school = new Dictionary<string, List<Student>>();

            Student myStudent = new Student("Jean", "Web");

            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);



            

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

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            _myMenu.Display();
            Selection();
        }
 
I have explained why you had the issue of only one student being in your dictionary. That doesn't mean that there aren't other issues with the code. They are not related to the issue you asked about though. If you have other issues, you really ought to create new threads for them: one topic per thread and one thread per topic. How to properly store the data for students and degrees is a different topic. You should also provide a title that is more descriptive of the actual issue.
 
I have explained why you had the issue of only one student being in your dictionary. That doesn't mean that there aren't other issues with the code. They are not related to the issue you asked about though. If you have other issues, you really ought to create new threads for them: one topic per thread and one thread per topic. How to properly store the data for students and degrees is a different topic. You should also provide a title that is more descriptive of the actual issue.
thank you very much. Will do.
 
Back
Top Bottom