Exploring Blockchain Development — Day 2
We are going to continue on from the previous article where we created our first contract. In this article, we will look at some of the different variables in Solidity for storing information.
Go ahead and create a new Solidity file and name it MyVariables.sol. Remember which lines of code you need at the beginning.
This is not our first variable you have created. In the previous article we create a string variable and assigned a value to it. Lets create an integer variable named myUInt. A uint is an alias for uint256 which is an unsigned integer, meaning it will accept positive numbers from 0 to 2²⁵⁶.
Save the file, compile and deploy the contract. Expand the MYVARIABLES contract and click on the myUInt button. You should see a message uint256: 0.
Note that uint is an alias for uint256 and that all variables are assigned a value when you initialise them. In this case, integers are assigned 0, strings — blank, bool — false.
A boolean can only have two values either true or false.
Type the following code
Lets break this down:
Line 7 : Declare a new bool variable named myBool that automatically gets assigned a value of false if not specified
Line 9 : Create a public function that passes a booleen value
Line 10 : Assign the passed value to the myBool variable
Save the file, and deploy contract.
As you keep deploying your contract, the previous versions are available, lets remove those earlier versions by clicking on the cross icon or you can click on the trash icon to remove all in the list and redeploy latest.
Click on the myBool button that shows you what the current value of false.
Lets set it to true by assigning the true value and calling the function. Click on the myBool button to confirm this variable has the value of true.
Now we will look at the Address type.
Type the following:
Again, we create and name a variable. We have a function that help assigns a new value to the variable. Deploy the contract and copy and pasted an address and assign it. Noticed how I copied the test wallet’s address.
The Address is a Type that has additional information you can access. Lets have a look how we may access the balance of an address.
Type the following code for the getBalanceForAddress function
Line 19 : we created a new function called getBalanceOfAddress that is a read-only function (doesn’t write anything) by using the view keyword and we use the returns keyword to specify we want to return an uint
Line 20: Return the balance of the address stored in the myAddress variable
Save and deploy your new version of your contract.
Strings are classified as Reference Type because we need to explicitly provide a data area where it is stored and strings are an array of characters.
Type the following:
Line 23: Declares a string variable
Line 25: We create a function that passes the memory location of the variable
If you save and deploy the new version of your contract, you can now assign a new string and get it from the variable. Please notice and when assigning a string variable, you need to surround the value with double quotes.
That covers the simply variables you can use in Solidity and we also had a look at using functions which we will cover more of in a later article. In the next article, we will create a simple smart contract to send ether to a contract and withdraw it.
If you found this article helpful, please give me a Clap, as this brightens my day knowing I may have helped someone in their journey in making stuff! Also if you want to be notified for future releases in this series, “Follow” me. You may also send me a message if you need any further help.