Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Enums in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enums
Define Enum with named values
↓
Use Enum value in code
↓
Compare or switch on Enum
↓
Execute code based on Enum case
↓
End
Enums let you create named constants to represent fixed options, making code clearer and safer.
Execution Sample
Blockchain / Solidity
enum Status {
  Pending,
  Confirmed,
  Failed
}

let txStatus = Status.Pending;
Defines a transaction status enum and sets a variable to the Pending status.
Execution Table
StepActionEnum StateVariable txStatusNotes
1Define enum Status with values Pending=0, Confirmed=1, Failed=2Status = {Pending:0, Confirmed:1, Failed:2}undefinedEnum created with named values
2Assign txStatus = Status.PendingStatus unchanged0txStatus holds enum value 0 (Pending)
3Check if txStatus == Status.PendingStatus unchanged0Condition true, code for Pending runs
4Update txStatus = Status.ConfirmedStatus unchanged1txStatus updated to Confirmed
5Check if txStatus == Status.FailedStatus unchanged1Condition false, code for Failed skipped
6End of exampleStatus unchanged1Execution stops
💡 Reached end of code after enum usage and checks
Variable Tracker
VariableStartAfter Step 2After Step 4Final
txStatusundefined0 (Pending)1 (Confirmed)1 (Confirmed)
Key Moments - 2 Insights
Why does txStatus hold a number instead of the word 'Pending'?
Enums assign numbers to names internally (Pending=0). txStatus stores the number for efficiency, but code uses the name for clarity (see execution_table step 2).
What happens if we compare txStatus to a wrong enum value?
The comparison fails because txStatus holds a specific number. For example, at step 5, txStatus=1 (Confirmed) is not equal to Failed=2, so that branch is skipped.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of txStatus after step 2?
A2 (Failed)
B1 (Confirmed)
C0 (Pending)
Dundefined
💡 Hint
Check the 'Variable txStatus' column at step 2 in the execution_table.
At which step does txStatus change from Pending to Confirmed?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Variable txStatus' column changes between steps in the execution_table.
If txStatus was set to Status.Failed at step 2, what would happen at step 3?
AThe condition txStatus == Status.Pending would be false
BThe condition txStatus == Status.Pending would be true
CtxStatus would automatically change to Pending
DThe program would crash
💡 Hint
Refer to how comparisons work in the execution_table and variable_tracker.
Concept Snapshot
Enums create named constants with fixed values.
Use enum names in code for clarity.
Internally, enums store numbers.
Compare enum variables to enum names.
Helps avoid magic numbers and bugs.
Full Transcript
Enums let you define a set of named values representing fixed options, like transaction statuses. In the example, we define an enum Status with Pending, Confirmed, and Failed. Each name maps to a number starting at 0. We assign txStatus to Status.Pending, which is 0 internally. Then we check txStatus against enum values to decide what code to run. Changing txStatus to Confirmed updates its value to 1. Comparing txStatus to a different enum value returns false if they don't match. This makes code easier to read and less error-prone than using raw numbers.

Practice

(1/5)
1. What is the main purpose of using enum in blockchain programming?
easy
A. To execute smart contracts faster
B. To create random numbers for cryptography
C. To store large amounts of data efficiently
D. To group related named values together for clarity and safety

Solution

  1. Step 1: Understand enum purpose

    Enums group related named values, making code easier to read and safer.
  2. Step 2: Compare options

    Only To group related named values together for clarity and safety correctly describes enums' main purpose in blockchain code.
  3. Final Answer:

    To group related named values together for clarity and safety -> Option D
  4. Quick Check:

    Enums group named values = A [OK]
Hint: Enums group fixed options with names, not data or speed [OK]
Common Mistakes:
  • Confusing enums with random number generators
  • Thinking enums store large data
  • Believing enums speed up contract execution
2. Which of the following is the correct syntax to declare an enum named Status with values Pending, Active, and Closed in Solidity?
easy
A. enum Status: Pending, Active, Closed
B. enum Status = (Pending, Active, Closed);
C. enum Status { Pending, Active, Closed }
D. enum Status { 'Pending', 'Active', 'Closed' }

Solution

  1. Step 1: Recall Solidity enum syntax

    In Solidity, enums are declared with enum Name { Value1, Value2, ... } without equals or colons.
  2. Step 2: Check each option

    enum Status { Pending, Active, Closed } matches correct syntax; others use invalid symbols or quotes.
  3. Final Answer:

    enum Status { Pending, Active, Closed } -> Option C
  4. Quick Check:

    Solidity enum syntax uses braces and commas = C [OK]
Hint: Enums use braces and commas, no equals or quotes [OK]
Common Mistakes:
  • Using equals sign instead of braces
  • Adding quotes around enum values
  • Using colons instead of braces
3. Given the Solidity enum declaration:
enum Role { User, Admin, Guest }

and the code:
Role userRole = Role.Admin;
if (userRole == Role.Guest) {
return "Guest Access";
} else {
return "Not Guest";
}

What will be the output?
medium
A. "Not Guest"
B. "Guest Access"
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand enum values and assignment

    Role.Admin is assigned to userRole, which is not equal to Role.Guest.
  2. Step 2: Evaluate the if condition

    The condition userRole == Role.Guest is false, so the else branch runs returning "Not Guest".
  3. Final Answer:

    "Not Guest" -> Option A
  4. Quick Check:

    Role.Admin != Role.Guest, so output = "Not Guest" [OK]
Hint: Compare enum values directly; Admin ≠ Guest means else runs [OK]
Common Mistakes:
  • Assuming Admin equals Guest
  • Expecting compilation or runtime errors
  • Confusing enum names with strings
4. Identify the error in this Solidity enum usage:
enum State { Created, Locked, Inactive }
State public state;
function setState(uint _state) public {
state = _state;
}
medium
A. Enum declaration syntax is wrong
B. Cannot assign uint directly to enum variable
C. Function setState should be private
D. Missing return type in function

Solution

  1. Step 1: Analyze enum assignment

    Enums require assignment from enum values, not raw uint integers.
  2. Step 2: Check function assignment

    Assigning _state (uint) directly to state (enum) causes a type mismatch error.
  3. Final Answer:

    Cannot assign uint directly to enum variable -> Option B
  4. Quick Check:

    Enum variables need enum values, not uints [OK]
Hint: Assign enum values, not integers, to enum variables [OK]
Common Mistakes:
  • Assigning integers directly to enums
  • Thinking enum syntax is wrong
  • Confusing function visibility with errors
5. You want to represent user roles in a blockchain contract using enums. You need to check if a user has either Admin or Moderator role to allow special access. Given:
enum Role { User, Admin, Moderator, Guest }

Which of the following Solidity code snippets correctly checks for this condition?
hard
A. if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ }
B. if (userRole == Role.Admin && userRole == Role.Moderator) { /* allow access */ }
C. if (userRole in [Role.Admin, Role.Moderator]) { /* allow access */ }
D. if (userRole.equals(Role.Admin) || userRole.equals(Role.Moderator)) { /* allow access */ }

Solution

  1. Step 1: Understand logical conditions for enums

    To allow access if userRole is Admin OR Moderator, use logical OR (||) between equality checks.
  2. Step 2: Evaluate each option

    if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } uses correct OR condition; B uses AND which is impossible; C uses invalid syntax; A uses non-existent method equals.
  3. Final Answer:

    if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } -> Option A
  4. Quick Check:

    Use || for OR between enum comparisons [OK]
Hint: Use || to check multiple enum values, not && or invalid syntax [OK]
Common Mistakes:
  • Using && instead of || for OR condition
  • Trying to use 'in' keyword for enums
  • Calling non-existent methods on enums
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard