What is Enum in programming
in simple and easy words with examples in Dart / Flutter
an enumeration, often referred to as “enum” for short, is a way to define a set of named values that represent a collection of related constants. Enums make your code more readable and maintainable by giving meaningful names to these constants.
Here’s a simple explanation using a basic example in Python:
Let’s say you’re creating a program that deals with different days of the week. Instead of using numbers to represent each day, you can use an enum to give each day a meaningful name:
enum Color {
red,
green,
blue,
yellow,
}
void main() {
Color myFavoriteColor = Color.blue;
if (myFavoriteColor == Color.red) {
print("Your favorite color is red!");
} else if (myFavoriteColor == Color.green) {
print("Your favorite color is green!");
} else if (myFavoriteColor == Color.blue) {
print("Your favorite color is blue!");
} else if (myFavoriteColor == Color.yellow) {
print("Your favorite color is yellow!");
} else {
print("Invalid color choice");
}
}
In this example, we first define the Color enum with four possible values: red, green, blue, and yellow. We then create a variable myFavoriteColor and assign the value Color.blue to it.
The if and else if statements are used to compare the value of myFavoriteColor with each of the enum values. If the condition is met, the corresponding message is printed. If none of the conditions match, the else block prints "Invalid color choice".
another Example
const today = DayOfWeek.WEDNESDAY
enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
}
void main() {
DayOfWeek today = DayOfWeek.WEDNESDAY;
if (today == DayOfWeek.WEDNESDAY) {
print("It's Wednesday!");
}
Example 2: Let’s dive into another example to further illustrate how enums work in Dart. In this example, we’ll create an enum to represent different types of fruits: apple, banana, orange, and grape.
enum Fruit {
APPLE,
BANANA,
ORANGE,
GRAPE,
}
class FruitInfo {
final Fruit fruit;
final String color;
final String taste;
FruitInfo(this.fruit, this.color, this.taste);
}
void main() {
List<FruitInfo> fruits = [
FruitInfo(Fruit.APPLE, "Red/Green", "Sweet and Crisp"),
FruitInfo(Fruit.BANANA, "Yellow", "Soft and Sweet"),
FruitInfo(Fruit.ORANGE, "Orange", "Juicy and Tangy"),
FruitInfo(Fruit.GRAPE, "Purple/Green", "Sweet and Small"),
];
Fruit selectedFruit = Fruit.APPLE;
for (var fruitInfo in fruits) {
if (fruitInfo.fruit == selectedFruit) {
print("Selected fruit: ${fruitInfo.fruit}");
print("Color: ${fruitInfo.color}");
print("Taste: ${fruitInfo.taste}");
break;
}
}
}
In this Dart example:
- We’ve defined an enum named
Fruit
to represent different types of fruits. - We’ve created a class
FruitInfo
that holds information about a fruit's name, color, and taste. - In the
main
function, we've created a list ofFruitInfo
objects, each containing details about a specific fruit. - We’ve initialized a
selectedFruit
variable to represent the fruit we want to get information about. - We iterate through the list of
FruitInfo
objects, compare theirfruit
value with theselectedFruit
, and print out information about the selected fruit.
This example showcases how enums can be used to categorize and manage different types of data in a structured and meaningful way, making the code more organized and easier to understand.