Back
Lesson 12:
Struct
Introduction to struct types and how to use it in your smart contracts.
Progress: 0%
Visit desktop version for better experiences.
Struct
Structs are custom defined types that can group several variables.
You can define the type by creating a struct
.
Structs can be declared outside of a contract and imported in another contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Tasks {
// Define a new struct type 'Task' with three fields.
struct Task {
string title;
string description;
bool done;
}
// Declare a dynamic array 'tasks' of 'Task' structs.
Task[] public tasks;
// Add a new task to the 'tasks' array.
function addTask(string calldata _title, string calldata _description) public {
// Create a new temporary 'Task' object in memory.
Task memory newTask;
newTask.title = _title;
newTask.description = _description;
newTask.done = false;
// Add the 'newTask' into the 'tasks' array.
tasks.push(newTask);
}
// Mark a task as done.
function markTaskAsDone(uint256 _index) public {
// Get a reference to the task in the 'tasks' array using the '_index'.
Task storage task = tasks[_index];
task.done = true;
}
// Get the details of a task.
function getTask(uint256 _index) public view returns (string memory title, string memory description, bool done) {
Task storage task = tasks[_index];
return (task.title, task.description, task.done);
}
// Update the title of a task.
function updateTaskTitle(uint256 _index, string calldata _newTitle) public {
Task storage task = tasks[_index];
task.title = _newTitle; // Set the new title of the task
}
// Update the description of a task.
function updateTaskDescription(uint256 _index, string calldata _newDescription) public {
Task storage task = tasks[_index];
task.description = _newDescription;
}
// Delete the task from the 'tasks' array using the '_index'.
// Note: The 'delete' keyword in Solidity doesn't actually delete the task from the 'tasks' array,
// it just sets the task at the given index to its initial default state (i.e., an empty string ("") for 'title' and 'description', and 'false' for 'done').
function deleteTask(uint256 _index) public {
delete tasks[_index];
}
}
Structs can also be declared and imported just like enum
.