Defining a Simple Enum
Part of the Logic & Flow section of Coddy's Dart journey — lesson 59 of 65.
Now that you understand what enums are, let's learn how to create them in Dart. The syntax is straightforward and follows a simple pattern.
To define an enum, you use the enum keyword followed by the name of your enum and a list of values inside curly braces:
enum Weather {
sunny,
cloudy,
rainy
}Each value in the enum is separated by a comma, and by convention, enum values are written in lowercase. The enum name itself should start with a capital letter, following Dart's naming conventions.
Once you've defined an enum, you can use it throughout your program. For example, you could create enums for user roles, game states, or priority levels:
enum Priority {
low,
medium,
high
}Creating an enum is that simple! You're essentially creating a new data type with a fixed set of possible values that will make your code more reliable and easier to understand.
Challenge
EasyCreate a program that defines an enum for different types of programming languages and demonstrates its usage. Your task is to:
- Define an enum called
ProgrammingLanguagewith the following values:dart,python,javascript,java, andcpp - Create a variable called
currentLanguageof typeProgrammingLanguageand assign it the valueProgrammingLanguage.dart - Create another variable called
nextLanguageof typeProgrammingLanguageand assign it the valueProgrammingLanguage.python - Print the current language with the exact format:
Currently learning: ProgrammingLanguage.dart - Print the next language with the exact format:
Next to learn: ProgrammingLanguage.python - Change the
currentLanguagetoProgrammingLanguage.javascript - Print the updated current language with the exact format:
Updated current language: ProgrammingLanguage.javascript
Your program should demonstrate the proper syntax for defining an enum using the enum keyword, declaring variables of enum type, and accessing enum values using dot notation. Make sure to follow Dart naming conventions where enum names start with a capital letter and enum values are written in lowercase.
Cheat sheet
To define an enum in Dart, use the enum keyword followed by the enum name and values in curly braces:
enum Weather {
sunny,
cloudy,
rainy
}Enum values are separated by commas and written in lowercase. Enum names should start with a capital letter following Dart naming conventions.
You can create variables of enum type and access values using dot notation:
enum Priority {
low,
medium,
high
}
Priority taskPriority = Priority.high;Try it yourself
// TODO: Define your ProgrammingLanguage enum here
void main() {
// TODO: Write your code here
// 1. Create currentLanguage variable and assign ProgrammingLanguage.dart
// 2. Create nextLanguage variable and assign ProgrammingLanguage.python
// 3. Print current language in the required format
// 4. Print next language in the required format
// 5. Update currentLanguage to ProgrammingLanguage.javascript
// 6. Print updated current language in the required format
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List6Basic Error Handling
What are Exceptions?The 'try-catch' BlockCatching Exceptions with 'on'The 'finally' BlockThrowing an ExceptionRecap - Safe Division9Enumerations (Enums)
What are Enums?Defining a Simple EnumUsing Enums in VariablesEnums in 'switch' StatementsRecap - Traffic Light