Variables, Data Types, and Custom Data Structures in Solidity.
There are two kinds of Variables in solidity. The local variables and the state variables. The local variable is created within a function and cannot be accessed by other functions outside. A function is created getvalue(). This function contains a local variable called value. This value will allow us to store a number and do things with it inside this function. Local Variables can be
Accessed only inside the getvalue() function. it can not be accessed outside the smart contract or other function.
The variable is not stored in the Blockchain, it is stored in the memory.
A state variable is a variable that has these characteristics:
It can be accessed outside of other functions.
It is stored on the Blockchain and not in memory like the local variable.
Let us see an example of a state variable.
In this example, the Public keyword shows that this variable can be read outside the smart contract.
- This variable can also be stored in the Blockchain.
Let us look at the various Data Types of solidity
String public myString = "Hello World";
This is a String called myString that is used to store text in Solidity.
int Public myInt = 1;
int means integer, it can be used to store integers, i.e numbers that can be either positive or negative.
uint public myUint = 1;
uint variable stores unsigned integer, i.e numbers without the negative sign e.g 1, 2, 3, 4.
uint256 public myUint256 = 2;
This is an unsigned integer of 256bytes. This is used when you want to store a large number. Also, note that the uint is also equal to the uint256.
uint8 public myUint8 = 1;
This is used to store an integer with 8bytes. This is used to store smaller numbers.
address public myAddress = 0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c;
This variable type is used to store an Ethereum address for an external user or smart contracts.
bytes32 public myBytes32 = "Hello World";
This is a variable type just like string but this is more performant
These are the basic variable types in solidity. Solidity also allows us to create a custom variable. This is called Struct. It allows us to create custom variables. Let's create a Struct in Solidity
The Struct is first created with the Struct keyword.
In the Struct created we declared two variables a unit and a string. So this struct allows us to store two different variable types. This is a very powerful feature of Solidity that can allow us to create our own custom data for any purpose that we want. For example, we can create a person struct with a name stored as a string and an address stored as an address.
This code snippet above just defines a new struct called Mystruct. To be able to create one more struct, we do it this way
This code creates a new struct with the string values and the unit value.
This is one of the very important features of the solidity programming language. To learn how to create your first smart contract using solidity, check out my Article on it How to create your first smart contract with solidity
To follow me on my journey to becoming a Blockchain developer and follow me on the #100dayscode challenge. Follow me on Twitter