Struct and Enum in Solidity

Struct and Enum in Solidity

A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs provide a way to define new data types. It introduces a new complex data type that is composed of other elementary data types. We use structs to represent a singular thing that has properties like a Car, a Person, a Request and so on, and we use mappings to represent a collection of things like collection of Cars, Persons, Requests.

A struct can be declared outside or inside of a contract. A struct is saved in storage and if declared inside a function it references storage by default. If declaring a struct outside of a contract allows it to be shared by multiple contracts. Let's declare such a struct.

No alt text provided for this image

In the same #solidity file we will create two contracts: Academy and School. Being declared outside of the contract, we can declare a variable of type Instructor in each contract. So Instructor, the type public, the name of the variable academyInstructor for the conctract Academy and the variable schoolInstructor for the contract School.

No alt text provided for this image

Now we will initialize the struct variable using the constructor (msg.sender is the address that deploys the contract).

No alt text provided for this image

Before testing the contract, we will declare a new function called changeInstructor to see how is modifying a struct variable. By default struct references storage. We will create a temporary memory struct (myInstructor) initializing it with the given values and copying it to the storage struct, the state variable of the contract. The syntax is looking like following. So we declare a new memory variable of type struct (Instructor) and name it myInstructor. This memory struct variable will be equal the name of the struct (Instructor), a pair of semicolon and inside the parentheses a pair of curly brackets, then age: _age, name: _name, addr: _addr.

No alt text provided for this image

Next we will copy the memory struct variable to the storage one.

No alt text provided for this image

Now let's test the part of the code about structs. We will give two arguments to the constructor and deploy it.

No alt text provided for this image

It was deployed. To see the struct we will click on the academyInstructor button.

No alt text provided for this image

We can see that everything is working fine - age is 40, name is John and the address is the account address. Now, let's change the struct from the changeInstructor function. We will write name Ben, age 35, and a random address.

No alt text provided for this image

Great! It is working, too :)

Now, let's go ahead and talk about enums. Enums are one way of creating a user-defined type in Solidity. They are explicitly convertible to and from integer types, but implicit conversion is not allowed. The word enum stands for enumerate. Enums are especially useful when we need to specify the current state or flow of a smart contract. The syntax is the same as in C language - we declare it in the contract, but enums can also be declared at the file level outside of a contract.

Let's write in our contract - enum followed by the new user defined type (State) and a pair of curly brackets. Also it is good to know that we don't need to end an enum with a semicolon. Think of enums as user-defined types that contain human-readable names for a set of constants, called members. After this, we will declare a new public state variable of type State which we will initialize it as State.Open;

No alt text provided for this image

In the changeInstructor function, we will test the academyState variable before modyfing the Instructor's data. So, we will change the Instructor's data, only if the academy is open. We will use an if statement.

No alt text provided for this image

You know that declaring a struct outside of a contract allows it to be shared by multiple contracts. So, our struct can be used in any contract declared in this file. You can experiment alone with the second contract (schoolInstructor) from our solidity file.

I uploaded this code to my GitHub account.

See you in future with articles about #solidity , #blockchain , #crypto , #web3 , #web3development , #defi .

To view or add a comment, sign in

More articles by Ion Platon

  • Storage vs Memory in Solidity

    Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is…

  • What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • What are Libraries in Solidity?

    Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other…

  • What are Interfaces in Solidity?

    Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics…

  • What is Inheritance in Solidity?

    Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in…

  • The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Bytes vs Strings in Solidity

    Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any…

  • Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

Explore content categories