0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 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.