Visibility & Structs In 'Solidity'(Learn 'Solidity' - Part 7)

in #ethereum7 years ago (edited)

After creating the contracts, if you look at the details, you won't find anything that describes your variables, i.e., name and age. This is where the visibility comes into play, like public, private, etc.

 

 

4 Types Of Visibility In Solidity For Variables & Functions

 

  • PUBLIC

It lets you define functions or variables that can be called internally or through messages.

 

  • PRIVATE

Private functions and variables are only available to the current contract and not to the derived contract.

 

  • INTERNAL

These are the variables or functions that can only be accessed internally.

 

  • EXTERNAL

These are the functions that can be called from the other contracts but they can't be called internally except 'this.' function name.

 
 

Let's add visibility to our variables with the 'public' keyword.

Capture5.JPG

 

 

Constructor

Every Smart Contract has a Constructor function. It is called when a contract is created. Let's create a Constructor Function which will execute when this Smart Contract will execute. You will need to use the name of your zsmart Contract for the constructor function.

 

Capture6.JPG

 

Constant Variables

In Solidity, Variables can be declared as constants and once you set them, you can't update their value anymore. So, let's change the variable 'fName' to a Constant Variable.

 

Capture7.JPG

 
 

Set A Variable

Let's try to set a variable by integrating a potential user interaction. Setting a variable will allow the user to input data. These variables get updated with the values provided by the user.

 
 

 
 


 

Previous Posts

 

Introduction To 'Solidity'(Learn 'Solidity' - Part 1)

 

Ethereum & Smart Contracts(Learn 'Solidity' - Part 2)

 

Tips To Learn 'Solidity' On Your Own(Learn 'Solidity - Part 3)

 

Basic Smart Contract(Learn 'Solidity' - Part 4)

 

Smart Contract VsnDApp(Learn 'Solidity' - Part 5)

 

Variables In 'Solidity'(Learn 'Solidity' - Part 6)

 


 

Upcoming Posts

 

Understanding ERC20 & ERC223 Tokens