#100DaysOfSolidity Solidity Structs: An Essential Guide ๐Ÿ“š #15

#100DaysOfSolidity Series 015 โ€œSolidity Structsโ€

Solidity Academy
4 min readJul 3, 2023

๐Ÿ‘‹ Welcome to another exciting article in the world of Solidity! In this post, weโ€™ll delve into the power of structs in Solidity and learn how they can revolutionize your smart contract development.

#100DaysOfSolidity Solidity Structs: An Essential Guide ๐Ÿ“š #15

So, letโ€™s dive in and explore the fascinating world of Solidity structs! ๐Ÿš€

## Understanding Structs ๐Ÿงฑ

In Solidity, structs allow you to create custom types that group related data together. They are similar to structs or classes in other programming languages. By encapsulating data into a single composite entity, structs help you organize and manage complex data structures within your contracts. Letโ€™s see how this works using an example. ๐Ÿ˜Š

Imagine we are building a todo list application in Solidity. Each todo item has a description and a completion status. We can define a struct called `Todo` to represent each todo item:

```solidity
struct Todo {
string text;
bool completed;
}
```

In the above code, we define a struct called `Todo` with two members: `text` of type `string` and `completed` of type `bool`. The `text` member represents the description of the todo item, while `completed` indicates whether the item has been completed or not. ๐Ÿ“โœ”๏ธ

## Creating and Accessing Structs โœจ

Now that we understand the basics of structs, letโ€™s dive into creating and accessing struct instances in Solidity. Weโ€™ll use the example of a todo list contract to illustrate the concepts. Hereโ€™s the code for the `Todos` contract:

contract Todos {
struct Todo {
string text;
bool completed;
}
Todo[] public todos;
function create(string calldata _text) public {
todos.push(Todo(_text, false));
todos.push(Todo({text: _text, completed: false}));
Todo memory todo;
todo.text = _text;
todos.push(todo);
}
function get(uint _index) public view returns (string memory text, bool completed) {
Todo storage todo = todos[_index];
return (todo.text, todo.completed);
}
function updateText(uint _index, string calldata _text) public {
Todo storage todo = todos[_index];
todo.text = _text;
}
function toggleCompleted(uint _index) public {
Todo storage todo = todos[_index];
todo.completed = !todo.completed;
}
}

The `Todos` contract demonstrates how to create, access, and modify instances of the `Todo` struct. Letโ€™s take a closer look at the key functions. ๐Ÿ”โœ๏ธ

### Creating Todo Items ๐Ÿ“

The `create` function allows you to create a new todo item by providing its text description. It demonstrates three different ways to initialize a `Todo` struct:

  1. Calling the struct like a function:

```solidity
todos.push(Todo(_text, false));
```

2. Using key-value mapping syntax:

```solidity
todos.push(Todo({text: _text, completed: false}));
```

3. Initializing an empty `Todo` struct and then updating its members:

```solidity
Todo memory todo;
todo.text = _text;
todos.push(todo);
```

These initialization methods offer flexibility based on your specific requirements. ๐ŸŽจ๐Ÿ› ๏ธ

### Accessing and Modifying Todo Items ๐Ÿ”๐Ÿ”ง

The `get` function allows you to retrieve the text description and completion status of a specific todo item, given its index in the `todos` array. It returns the values as a tuple, which includes the text description and a boolean indicating completion.

The `updateText` function enables you to modify the text description of a todo item. You can pass the index of the item to be updated and the new text as parameters. The function locates the corresponding `Todo` struct in the `todos` array using the index and modifies the `text` member accordingly.

The `toggleCompleted` function toggles the completion status of a todo item. Similar to `updateText`, you provide the index of the item, and the function modifies the `completed` member accordingly. ๐Ÿ”„โœ๏ธ

## Importing Structs ๐Ÿ“ฅ

Structs can also be declared in separate files and imported into other contracts, promoting code organization and reusability. Letโ€™s explore how to declare and import structs across multiple files. ๐Ÿ“„๐Ÿ“ฆ

### Struct Declaration File ๐Ÿ“

In a file named `StructDeclaration.sol`, we can declare the `Todo` struct as follows:

```solidity
struct Todo {
string text;
bool completed;
}
```

The `StructDeclaration.sol` file solely contains the struct declaration, which is placed at the top-level scope. ๐Ÿ“„โœ๏ธ

### Importing the Struct into a Contract ๐Ÿ“ฅ

To import the `Todo` struct from `StructDeclaration.sol`, we use the import statement within another contract file:

```solidity
import โ€œ./StructDeclaration.solโ€;

contract Todos {
StructDeclaration.Todo[] public todos;
}
```

By importing the struct, you can access its members and utilize them within your contract. This practice promotes code reuse and enhances the maintainability of your Solidity projects. โ™ป๏ธ๐Ÿ”ง

## Conclusion ๐ŸŽ‰

Congratulations! You now have a solid understanding of structs in Solidity. Structs are an incredibly useful tool for organizing and managing related data within your smart contracts. Youโ€™ve learned how to define and initialize structs, as well as create, access, and modify struct instances. Additionally, you explored how to import structs from separate files, enabling code reusability and better organization. ๐Ÿ“š๐Ÿ’ช

Now itโ€™s time to leverage the power of structs in your own Solidity projects! Experiment with different scenarios and use cases to enhance your contract development skills. Stay tuned for more exciting Solidity content, and happy coding! ๐ŸŽ‰๐Ÿ’ป

๐Ÿ”— Additional Resources:

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/