Visual C# | Inheritance exercise | Create a to-do list with recurring tasks using inheritance

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Visual C#  (79 exercises)
Version ==> Any version of C#
Topic ==> Inheritance  (5 exercises)
Level ==> Average difficulty
Subject ==> C# training

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

The aim of this exercise is to be able to create a sorted dictionary:

Sorted dictionary

Create this so that we can later sort the tasks by date.

You should then be able to create new Task objects:

Task t1 = new Task(DateTime.Now.AddDays(7), "Give up smoking");



Task t2 = new Task(DateTime.Now.AddDays(20), "Give up drinking");



Task t3 = new Task(DateTime.Now.AddDays(11), "Audtion for BGT");

t3.Priority = Task.TaskPriority.High;


You can then loop over the dictionary to get the tasks in sorted order:

Listing out tasks from dictionary

This code would loop over the dictionary, showing the date and time for each task.

When this is all working, the exercise will then ask you to create a RecurringTask class.  First things first!

Creating a Task Class 

Create a Task class to hold the details of each task.  You could include the following two enumerations:

public enum TaskPriority { Low, Medium, High }

public enum TaskRecurrence {

Once=0, Day=1, Week=7, Month=30, Year=365 }

By default, tasks should have Medium priority and Once task recurrence.

When code consuming the task calls the Create method, this should add the current instance of the class to a static list of type Task.  You'll need to create a constructor and propertes to get the code shown at the start of this exercise to work!

Recurring Tasks

When you've got all of the above working, add a RecurringTask class which inherits from the Task class.  Include this in your code so that the new version allows you to add recurring tasks:

// cat needs feeding every week

Task t4 = new RecurringTask(System.DateTime.Today.AddDays(6), "Feed the cat");

t4.Recurrence = Task.TaskRecurrence.Week;


When you add your 4 tasks (the 3 previous ones and this recurring one), you should see them sorted in date order:

List of tasks including recurring

The tasks you've created - recurring ones recur, as you'd expect!


Save and close any files, and give yourself another big pat on the back! 

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post