This is the solution to the challenge called ChallengeStudentCourses, which was designed to test your knowledge of Collections, Object Initializers, Lists and Dictionaries. As always, if you're having trouble completing the challenge, read on to that specific area to solve the issue you're having. Try not to read more than you need in order to proceed, and do as much of the challenge as you can on your own.
Included with the challenge was a folder called CS-ASP_051-Challenge_Code, which contains the starting point for the challenge itself. Download and unzip this folder, then open the .sln file inside to begin your project. Inside the Default.aspx, you'll find three buttons, a resultLabel, and in Default.aspx.cs, there are pre-generated events for each button, along with instructions for each contained in comments:
In addition to this, there are two additional classes created: Student and Course. Each have preset properties that you will utilize throughout this challenge.
The instructions for the first challenge tell us that we need to create a List<Course> and populate it with three Courses that we will create. In addition to that, each course should have at least two Students enrolled in them. The objective is to print out the details of each course as well as the students enrolled into the resultLabel.
To begin, we'll create a List<Course> called courses. Using object initializers, we'll populate the List with three different courses:
Now that we've populated the List with three Courses, each with a CourseId and a Name, we need to give each Course a List<Student>. To do this, we'll use another Collection Initializer within this one for the Student property:
Notice the use of multiple initializers within the same statement. We are able to initialize a List<Student> at the same time we are initializing the very Course it will be placed in. This allows the Students property to be initialized at its creation and saves extra steps when programming. However, this is not necessarily the best way to do this, as it results in extra indentation and nested code.
Now that the List has been created and its properties populated, we can iterate through it in order to display its contents. To do this, we'll use two foreach() statements; one evaluating each course, and one for each student. This will add to the resultLabel's text, and then display the result:
Notice that the foreach() statement for students is located within the foreach() statement for courses. The code will evaluate one course, print out its CourseId and Name properties, then it will initiate the foreach() inside it, looking for each student within that course and displaying the StudentId and Name to the resultLabel.
This process will repeat until all courses within the List<Course> have been evaluated, resulting in the following:
In the assignment2Button_Click event, we are given the following instructions for the next challenge:
To begin with, we'll need to create a Dictionary<int, Student> called students with three Students. The key is set to an integer because we are directed to use the StudentId as the key. Create this, and initialize three Students as follows:
This fulfills two of the three requirements for this challenge, but we still need to give each Student two courses. Since our courses List was locally-scoped, we need to create new courses for this collection of students:
Now, inside the students Dictionary we created, add two of these courses to the Courses property by creating a new List<Course>:
Now that we've created the Dictionary and it's been populated, we need to display the details to the user. In order to do this, use another set of foreach() statements in a similar manner to the first challenge. The first will iterate through the students in the Dictionary, then the second will iterate through the courses in each student:
Save and run your project, click on the Assignment 2 button and see the result:
The third challenge, located under the assignment3Button_Click, requires us to track each student's grade, as follows:
To begin with, we need to create a new class as directed by the challenge's requirements. To do this, right-click on your project in the Solution Explorer and select Add > Class... In the dialogue window that appears, select Class and call the class Enrollment.cs.
This Enrollment class will need to know three things: The course, student and grade for the course. We'll create three properties within the class to meet these needs:
The last thing to do is modify the Student class by giving it a property for a List<Enrollment> called Enrollments:
In the assingment3Button_Click, we'll need to create a new instance of Student called student with a StudentId of 10 and a name "Pete Wentz". Once it's created, set student.Enrollments equal to a new List<Enrollments>:
Now we'll populate that List with courses, setting the CourseId and Name properties as follows:
Next, prepend the Grade property to each member of the collection. For our purposes, we will hard-code the grade values:
Finally, we'll need to display the student's name, course and grade details to the user in the resultLabel. This time, the process will be simpler because there is only one student. Set the resultLabel's Text property to the following:
Then, create a foreach() statement underneath this, evaluating each enrollment in student.Enrollments. Its function will be nearly identical to the previous foreach() statements we've created:
Now save and run your project to see the result:
This completes the solution to ChallengeStudentCourses. Hopefully you were able to figure out most if not all of the details needed to complete this challenge. While it wasn't intensive, it did require you to know how to use the List and Dictionary collections and know how to initialize the objects and properties within them. If you didn't complete this on your own, go back and try again at a different time. Cement these concepts in your mind before moving on to the next MegaChallenge. Good job!
Solution - Challenge Student Courses