display the name of the student with degree?

Jean101

Member
Joined
Feb 8, 2020
Messages
10
Programming Experience
1-3
How to properly display the name of the student with degree in mobile only in a private Dictionary<string, string> _studentDegree = new Dictionary<string, string>();

I'm having a hard time coming with a way to just show mobile student. Hours staring at the screen with blank ideas.

C#:
// This method work perfectly
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;
            Student myStudent = new Student();


            int counter = 0;
            Console.WriteLine($"{"  "} {"Name", -15} {"Degree", -10}");
            foreach (KeyValuePair<string, string> kvp in _studentDegree)
            {

                counter++;
                Console.WriteLine($"{counter}. {kvp.Key, -15} {kvp.Value, -10}");
                //myStudent.GenerateGrade();
            }

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

// Output: 
Show All Students: 

   Name            Degree    
1. JEAN            MOBILE    
2. MAX             WEB       
3. LU              MOBILE    
4. TOM             WEB       

Press any key to continue...


//End of method



// Only display mobile student

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



            
            
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
            _myMenu.Display();
            Selection();
        }
 
The point of a Dictionary is that you can specify a key and quickly get the corresponding value. In your case, your unique keys are student names and the values are the degree that each student is enrolled in. That means that your Dictionary is only being used properly if you are providing a student name and getting the corresponding degree. If you want to be able to specify a degree and get the students enrolled in it then you can use what you have but you're not getting the benefit of the fast lookups that a Dictionary provides. You'd either have to replace what you have or create a second Dictionary where the keys were the degree names and the values are List<string> containing the names of the students enrolled in that degree. You did specifically say that you wanted the student names to be the keys.

As it stands, what you could do is just treat the Dictionary as a simple collection and forgo the fast lookups. In this instance, you probably won't notice any difference anyway. You can loop through the KeyValuePair<string, string> objects in the list and test whether the Value is the degree of interest. If it is, put the Key into another list. When you're done, that other list contains the names of all students in the degree of interest.
 
The point of a Dictionary is that you can specify a key and quickly get the corresponding value. In your case, your unique keys are student names and the values are the degree that each student is enrolled in. That means that your Dictionary is only being used properly if you are providing a student name and getting the corresponding degree. If you want to be able to specify a degree and get the students enrolled in it then you can use what you have but you're not getting the benefit of the fast lookups that a Dictionary provides. You'd either have to replace what you have or create a second Dictionary where the keys were the degree names and the values are List<string> containing the names of the students enrolled in that degree. You did specifically say that you wanted the student names to be the keys.

As it stands, what you could do is just treat the Dictionary as a simple collection and forgo the fast lookups. In this instance, you probably won't notice any difference anyway. You can loop through the KeyValuePair<string, string> objects in the list and test whether the Value is the degree of interest. If it is, put the Key into another list. When you're done, that other list contains the names of all students in the degree of interest.
Oh thank you i completely see what you saying. I got it working. All i had to do was to the call the value instead of the key.
 
Back
Top Bottom