Menu
Coddy logo textTech

Using Enums in Variables

Part of the Logic & Flow section of Coddy's Dart journey — lesson 60 of 65.

Now that you know how to define enums, let's learn how to actually use them in your programs. Once you've created an enum, you can declare variables of that enum type and assign specific values to them.

To declare a variable of an enum type, you specify the enum name followed by the variable name, just like with any other data type:

Weather todayWeather = Weather.sunny;

Notice how you access enum values using dot notation: Weather.sunny. This tells Dart that you want the sunny value from the Weather enum.

You can change the variable's value to any other valid enum value:

todayWeather = Weather.rainy;
todayWeather = Weather.cloudy;

The beauty of using enum variables is that Dart ensures you can only assign valid enum values. You cannot accidentally assign "stormy" or any other invalid value to a Weather variable, which prevents common programming errors and makes your code more reliable.

challenge icon

Challenge

Easy

Create a program that manages a simple task tracking system using enums and variables. Your task is to:

  1. Define an enum called TaskStatus with the following values: todo, inProgress, testing, completed, and cancelled
  2. Create a variable called task1Status of type TaskStatus and assign it the value TaskStatus.todo
  3. Create a variable called task2Status of type TaskStatus and assign it the value TaskStatus.inProgress
  4. Create a variable called task3Status of type TaskStatus and assign it the value TaskStatus.completed
  5. Print the initial status of all tasks with the exact format: Task 1 status: TaskStatus.todo
  6. Print the initial status of task 2 with the exact format: Task 2 status: TaskStatus.inProgress
  7. Print the initial status of task 3 with the exact format: Task 3 status: TaskStatus.completed
  8. Update task1Status to TaskStatus.inProgress
  9. Update task2Status to TaskStatus.testing
  10. Print the updated status of task 1 with the exact format: Updated Task 1 status: TaskStatus.inProgress
  11. Print the updated status of task 2 with the exact format: Updated Task 2 status: TaskStatus.testing
  12. Print the final status of task 3 with the exact format: Final Task 3 status: TaskStatus.completed

Your program should demonstrate proper enum definition using the enum keyword, variable declaration with enum types, accessing enum values using dot notation, and updating enum variables. Make sure to follow Dart naming conventions where the enum name starts with a capital letter and enum values are written in lowercase.

Cheat sheet

To declare a variable of an enum type, specify the enum name followed by the variable name:

Weather todayWeather = Weather.sunny;

Access enum values using dot notation with the format EnumName.value:

todayWeather = Weather.rainy;
todayWeather = Weather.cloudy;

Dart ensures you can only assign valid enum values to enum variables, preventing invalid assignments and making code more reliable.

Try it yourself

import 'dart:io';

// TODO: Define your TaskStatus enum here

void main() {
  // TODO: Write your code here
  // 1. Create task1Status variable and assign TaskStatus.todo
  // 2. Create task2Status variable and assign TaskStatus.inProgress  
  // 3. Create task3Status variable and assign TaskStatus.completed
  // 4. Print initial status of all tasks
  // 5. Update task statuses as required
  // 6. Print updated and final statuses
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow