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.
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.
Now we will initialize the struct variable using the constructor (msg.sender is the address that deploys the contract).
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.
Next we will copy the memory struct variable to the storage one.
Now let's test the part of the code about structs. We will give two arguments to the constructor and deploy it.
It was deployed. To see the struct we will click on the academyInstructor button.
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.
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;
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.
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 .