Enumerations

Summary

The name 'Enumeration' already explains what it is. For example the enumeration COLOR can include RED, BLUE and GREEN.

An enumeration you already have used is the Boolean. In the Boolean you can choose between TRUE and FALSE.

Coding:

First, create a new console application.

Beneath the program class we are creating a class called "Todo" with the enumeration "Status".

The syntax for the enumeration looks like this:   enum <name>  { <list of values>  }

class Todo
{
public string Description { get; set; }
public int EstimatedHours { get; set; }
public Status Status { get; set; }
}

enum Status
{
NotStarted,
InProgress,
OnHold,
Completed,
Deleted
}

We have the following situation: We want to print all names of a list of Todo's into the console and color each Todo by their status.

We are going to run through the list in a loop and make a decision for the color by using the switch-statement.

But first, we are creating the list of Todo's and call a function which will print our Todo's into the console:

static void Main(string[] args)
{
List<Todo> todos = new List<Todo>()
{
new Todo { Description = "Task 1", EstimatedHours = 6, Status = Status.Completed },
new Todo { Description = "Task 2", EstimatedHours = 2, Status = Status.InProgress },
new Todo { Description = "Task 3", EstimatedHours = 8, Status = Status.NotStarted },
new Todo { Description = "Task 4", EstimatedHours = 12, Status = Status.Deleted },
new Todo { Description = "Task 5", EstimatedHours = 6, Status = Status.InProgress },
new Todo { Description = "Task 6", EstimatedHours = 2, Status = Status.NotStarted },
new Todo { Description = "Task 7", EstimatedHours = 14, Status = Status.NotStarted },
new Todo { Description = "Task 8", EstimatedHours = 8, Status = Status.Completed },
new Todo { Description = "Task 9", EstimatedHours = 8, Status = Status.InProgress },
new Todo { Description = "Task 10", EstimatedHours = 8, Status = Status.Completed },
new Todo { Description = "Task 11", EstimatedHours = 4, Status = Status.NotStarted },
new Todo { Description = "Task 12", EstimatedHours = 10, Status = Status.Completed },
new Todo { Description = "Task 13", EstimatedHours = 12, Status = Status.Deleted },
new Todo { Description = "Task 14", EstimatedHours = 6, Status = Status.Completed }
};

PrintAssessment(todos);
Console.ReadLine();
}

Now, we create the PrintAssessment method:

In the case stands our value of the enumeration and not like in one of our previous tutorials an integer or string.

private static void PrintAssessment(List<Todo> todos)
{
foreach (var todo in todos)
{
switch (todo.Status)
{
case Status.NotStarted:
Console.ForegroundColor = ConsoleColor.Red;
break;
case Status.InProgress:
Console.ForegroundColor = ConsoleColor.Green;
break;
case Status.OnHold:
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
case Status.Completed:
Console.ForegroundColor = ConsoleColor.Blue;
break;
case Status.Deleted:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
default:
break;
}
Console.WriteLine(todo.Description);
}
}

Complete program code:

class Program
{
static void Main(string[] args)
{
List<Todo> todos = new List<Todo>()
{
new Todo { Description = "Task 1", EstimatedHours = 6, Status = Status.Completed },
new Todo { Description = "Task 2", EstimatedHours = 2, Status = Status.InProgress },
new Todo { Description = "Task 3", EstimatedHours = 8, Status = Status.NotStarted },
new Todo { Description = "Task 4", EstimatedHours = 12, Status = Status.Deleted },
new Todo { Description = "Task 5", EstimatedHours = 6, Status = Status.InProgress },
new Todo { Description = "Task 6", EstimatedHours = 2, Status = Status.NotStarted },
new Todo { Description = "Task 7", EstimatedHours = 14, Status = Status.NotStarted },
new Todo { Description = "Task 8", EstimatedHours = 8, Status = Status.Completed },
new Todo { Description = "Task 9", EstimatedHours = 8, Status = Status.InProgress },
new Todo { Description = "Task 10", EstimatedHours = 8, Status = Status.Completed },
new Todo { Description = "Task 11", EstimatedHours = 4, Status = Status.NotStarted },
new Todo { Description = "Task 12", EstimatedHours = 10, Status = Status.Completed },
new Todo { Description = "Task 13", EstimatedHours = 12, Status = Status.Deleted },
new Todo { Description = "Task 14", EstimatedHours = 6, Status = Status.Completed }
};

Console.ForegroundColor = ConsoleColor.DarkRed;

PrintAssessment(todos);
Console.ReadLine();
}

private static void PrintAssessment(List<Todo> todos)
{
foreach (var todo in todos)
{
switch (todo.Status)
{
case Status.NotStarted:
Console.ForegroundColor = ConsoleColor.Red;
break;
case Status.InProgress:
Console.ForegroundColor = ConsoleColor.Green;
break;
case Status.OnHold:
Console.ForegroundColor = ConsoleColor.DarkRed;
break;
case Status.Completed:
Console.ForegroundColor = ConsoleColor.Blue;
break;
case Status.Deleted:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
default:
break;
}
Console.WriteLine(todo.Description);
}
}
}

class Todo
{
public string Description { get; set; }
public int EstimatedHours { get; set; }
public Status Status { get; set; }
}

enum Status
{
NotStarted,
InProgress,
OnHold,
Completed,
Deleted
}