0Pricing
Blockchain Smart Contracts with Solidity · Lesson

Structs and Enums

Model complex data in Solidity using custom struct types and represent fixed sets of states with enums.

Structs and Enums is a free Blockchain Smart Contracts with Solidity lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Blockchain Smart Contracts with Solidity learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Custom Types?

Beyond primitives like uint and address, Solidity lets you define your own types. Structs group related fields, and enums represent a fixed set of named states.

Declaring a Struct

A struct bundles several variables under one name, perfect for modeling real-world entities like a user or an order.

struct User {
    string name;
    uint age;
    address wallet;
}

Creating Struct Instances

You can construct a struct by positional arguments or by naming each field. Named arguments are clearer and less error prone.

User memory u = User({
    name: "Alice",
    age: 30,
    wallet: msg.sender
});

Structs in Storage vs Memory

A struct in storage persists on chain; in memory it is a temporary copy. Assigning a storage struct to a memory variable copies it, so changes do not write back.

Updating Struct Fields

To modify persisted data, work with a storage reference and assign to its fields directly.

User storage u = users[msg.sender];
u.age = 31;

Structs Inside Mappings

Structs shine when stored in a mapping keyed by address or id, giving each user their own record.

mapping(address => User) public users;
users[msg.sender] = User("Bob", 25, msg.sender);

Declaring an Enum

An enum defines a small set of named options. It improves readability over raw numbers for representing state.

enum Status { Pending, Shipped, Delivered, Cancelled }

Using Enum Values

Enum members are stored as uint starting at 0. You compare and assign them by name, which makes contract logic self-documenting.

Status public status = Status.Pending;

function ship() public {
    status = Status.Shipped;
}

Enums in State Machines

Enums are ideal for modeling a state machine. Guard transitions with require so an order can only move to a valid next state.

function deliver() public {
    require(status == Status.Shipped, "Not shipped yet");
    status = Status.Delivered;
}

Combining Structs and Enums

Put an enum field inside a struct to track each record state, for example storing a Status on every order struct so each order tracks its own lifecycle.

Gas Considerations

Each struct field is a storage slot, and writing storage is expensive. Order fields to pack smaller types together, and use enums (one byte range) instead of strings for fixed states to save gas.

Quick Check

Check your structs and enums knowledge.

Recap

You learned Solidity custom types:

  • Structs group related fields and are often stored in mappings
  • Mind storage vs memory when copying or updating structs
  • Enums model fixed states as integers and power state machines
  • Combine them and pack fields to save gas

Custom types make contracts more expressive and maintainable.

Frequently asked questions

Is the “Structs and Enums” lesson free?

Yes — the full text of “Structs and Enums” is free to read here on the web, and the Blockchain Smart Contracts with Solidity course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Blockchain Smart Contracts with Solidity course, upgrade to CoddyKit PRO.

What will I learn in “Structs and Enums”?

Model complex data in Solidity using custom struct types and represent fixed sets of states with enums. You practise Blockchain Smart Contracts with Solidity with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Blockchain Smart Contracts with Solidity?

No prior experience is required. Blockchain Smart Contracts with Solidity on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Structs and Enums” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Blockchain Smart Contracts with Solidity lesson?

Yes. Every Blockchain Smart Contracts with Solidity lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Solidity Data Types and Variables
  2. Control Structures and Loops
  3. Functions and Visibility Modifiers
  4. Structs and Enums
← Back to Blockchain Smart Contracts with Solidity