Solution - Challenge Student Courses

Tutorial Series: Free C# Fundamentals via ASP.NET Web Apps

Previous Article  |  Next Article


Get GitHub Code

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.

Introduction : Setup the Project

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:


Instructions


In addition to this, there are two additional classes created: Student and Course. Each have preset properties that you will utilize throughout this challenge.

Challenge 1

Step 1: Create a List<Course>

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:


ListCourseNoStudent


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:


ListCourseWithStudent


Tip:

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.

Step 2: Display Course Details

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:


foreachCourse


foreachStudent


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:


Challenge1Result


Challenge 2

In the assignment2Button_Click event, we are given the following instructions for the next challenge:


Challenge2


Step 1: Create a Dictionary<Student>

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:


DictionaryStudentNoCourse


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:


studentCourses


Now, inside the students Dictionary we created, add two of these courses to the Courses property by creating a new List<Course>:


DictionaryStudentWithCourse


Step 2: Display Student Details

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:


foreachStudents


Save and run your project, click on the Assignment 2 button and see the result:


Challenge2Result


Challenge 3

The third challenge, located under the assignment3Button_Click, requires us to track each student's grade, as follows:


Challenge3


Step 1: Setup the Classes

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:


EnrollmentProperties


The last thing to do is modify the Student class by giving it a property for a List<Enrollment> called Enrollments:


StudentEnrollments


Step 2: Creating List<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>:


ListEnrollments


Now we'll populate that List with courses, setting the CourseId and Name properties as follows:


EnrollmentsNoGrade


Next, prepend the Grade property to each member of the collection. For our purposes, we will hard-code the grade values:


ListEnrollmentsGrade


Step 3: Display Student Course Grades

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:


StudentResultLabel


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:


foreachEnrollment


Now save and run your project to see the result:


Challenge3Result


Conclusion

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!


Related Articles in this Tutorial:

Lesson 1 - Series Introduction

Lesson 2 - Installing Visual Studio 2015

Lesson 3 - Building Your First Web App

Lesson 4 - Understanding What You Just Did

Lesson 5 - Working with Projects in Visual Studio

Lesson 6 - Simple Web Page Formatting in Visual Studio

Challenge 1

Solution 1

Lesson 7 - Variables and Data Types

Lesson 8 - Data Type Conversion

Lesson 9 - Arithmetic Operators

Lesson 10 - C# Syntax Basics

Challenge 2 - ChallengeSimpleCalculator

Solution - ChallengeSimpleCalculator

Lesson 11 - Conditional If Statements

Lesson 12 - The Conditional Ternary Operator

Challenge 3 - ChallengeConditionalRadioButton

Solution - Challenge Conditional RadioButton

Lesson 13 - Comparison and Logical Operators

Lesson 13 Challenge - First Papa Bob's Website

Solution - Challenge First Papa Bob's Website

Lesson 14 - Working with Dates and Times

Lesson 15 - Working With Spans of Time

Lesson 16 - Working with the Calendar Server Control

Challenge 4 - Challenge Days Between Dates

Solution - Challenge Days Between Dates

Lesson 17 - Page_Load and Page.IsPostBack

Lesson 18 - Setting a Break Point and Debugging

Lesson 19 - Formatting Strings

Challenge 5 - Challenge Epic Spies Assignment

Solution - Challenge Epic Spies Assignment

Lesson 20 - Maintaining State with ViewState

Lesson 21 - Storing Values in Arrays

Lesson 22 - Understanding Multidimensional Arrays

Lesson 23 - Changing the Length of an Array

Challenge 6 - Challenge Epic Spies Asset Tracker

Solution - Challenge Epic Spies Asset Tracker

Lesson 24 - Understanding Variable Scope

Lesson 25 - Code Blocks and Nested If Statements

Lesson 26 - Looping with the For Iteration Statement

Challenge 7 - Challenge For Xmen Battle Count

Solution - Challenge For Xmen Battle Count

Lesson 27 - Looping with the while() & do...while() Iteration Statements

Lesson 28 - Creating and Calling Simple Helper Methods

Lesson 29 - Creating Methods with Input Parameters

Lesson 30 - Returning Values from Methods

Lesson 31 - Creating Overloaded Methods

Lesson 32 - Creating Optional Parameters

Lesson 33 - Creating Names Parameters

Lesson 34 - Creating Methods with Output Parameters

Challenge 8 - Challenge Postal Calculator Helper Methods

Solution - Challenge Postal Calculator Helper Methods

Mega Challenge Casino

Solution - Mega Challenge Casino

Lesson 35 - Manipulating Strings

Challenge 9 - Phun With Strings

Solution - Challenge Phun With Strings

Lesson 36 - Introduction to Classes and Objects

Challenge - Hero Monster Classes Part 1

Solution - Hero Monster Classes Part 1

Challenge - Hero Monster Classes Part 2

Solution - Challenge Hero Monster Classes Part 2

Lesson 37 - Creating Class Files Creating Cohesive Classes and Code Navigation

Lesson 38 - Understanding Object References and Object Lifetime

Lesson 39 - Understanding the .NET Framework and Compilation

Lesson 40 - Namespaces and Using Directives

Lesson 41 - Creating Class Libraries and Adding References to Assemblies

Lesson 42 - Accessibility Modifiers, Fields and Properties

Lesson 43 - Creating Constructor Methods

Lesson 44 - Naming Conventions for Identifiers

Lesson 45 - Static vs Instance Members

Challenge 10 - Challenge Simple Darts

Solution - Challenge Simple Darts

Lesson 46 - Working with the List Collection

Lesson 47 - Object Initializers

Lesson 48 - Collection Initializers

Lesson 49 - Working with the Dictionary Collection

Lesson 50 - Looping with the foreach Iteration Statement

Lesson 51 - Implicitly-Typed Variables with the var Keyword

Challenge 11 - Challenge Student Courses

Solution - Challenge Student Courses

Mega Challenge War

Solution - Mega Challenge War

Lesson 52 - Creating GUIDs

Lesson 53 - Working with Enumerations

Lesson 54 - Understanding the switch() Statement

Lesson 55 - First Pass at the Separation of Concerns Principle

Lesson 56 - Understanding Exception Handling

Lesson 57 - Understanding Global Exception Handling

Lesson 58 - Understanding Custom Exceptions

Lesson 59 - Creating a Database in Visual Studio

Lesson 60 - Creating an Entity Data Model

Lesson 61 - Displaying the DbSet Result in an ASP.NET GridView

Lesson 62 - Implementing a Button Command in a GridView

Lesson 63 - Using a Tools-Centric Approach to Building a Database Application

Lesson 64 - Using a Maintenance-Driven Approach to Building a Database Application

Lesson 65 - Creating a New Instance of an Entity and Persisting it to the Database

Lesson 66 - Package Management with NuGet

Lesson 67 - NuGet No-Commit Workflow

Lesson 68 - Introduction the Twitter Bootstrap CSS Framework

Lesson 69 - Mapping Enum Types to Entity Properties in the Framework Designer

Lesson 70 - Deploying the App to Microsoft Azure Web Services Web Apps

Papa Bob's Mega Challenge

Papa Bob's Mega Solution Part 1 - Setting up the Solution

Papa Bob's Mega Solution Part 2 - Adding an Order to the Database

Papa Bob's Mega Solution Part 3 - Passing an Order from the Presentation Layer

Papa Bob's Mega Solution Part 4 - Creating the Order Form

Papa Bob's Mega Solution Part 5 - Adding Enums

Papa Bob's Mega Solution Part 6 - Creating an Order with Validation

Papa Bob's Mega Solution Part 7 - Calculating the Order Price

Papa Bob's Mega Solution Part 8 - Displaying the Price to the User

Papa Bob's Mega Solution Part 9 - Creating the Order Management Page


Comments

Please login or register to add a comment