Solidity – Variables

In Solidity, variables are used to store data within a contract. Variables can be declared using the keywords “var” or “let” and are assigned a data type, such as “uint” for unsigned integers or “address” for Ethereum addresses.

Here is an example of declaring variables in a contract:

pragma solidity ^0.8.0;

contract MyContract {
    address public owner;
    uint public age;
    string public name;
    mapping(address => uint) public balance;
    bool public isValid;
    ...
}

In this example, a contract called “MyContract” is defined and it has five variables:

  • “owner” of type “address”
  • “age” of type “uint”
  • “name” of type “string”
  • “balance” of type “mapping”
  • “isValid” of type “bool”

It’s worth noting that variables can also be declared with visibility level “public”, “private” or “internal” which determines the accessibility of the variable from outside of the contract. Public variables can be accessed by any address, private variables can only be accessed by the contract’s functions, and internal variables can only be accessed by other contracts that inherit or are part of the same contract.

Also, Solidity supports constant variables, which are variables whose values cannot be modified once they are set. The keyword “constant” or “const” is used to define a constant variable.

pragma solidity ^0.8.0;

contract MyContract {
    address public owner;    uint public age;
    string public name;
    mapping(address => uint) public balance;
    bool public isValid;
    string constant version = "1.0.0";
    ...
}

Overall, variables are an essential element of Solidity, they are used to store data within a contract, and can be used to keep track of the state of the contract. It’s important to use the right type for a variable, and to use visibility level and constant level appropriately according to the contract’s logic.

Solidity supports three types of variables:

In Solidity, there are three types of variables: static, local, and global variables. Each type has its own characteristics and uses.

  • Static variables: Static variables are declared using the “static” keyword and are shared among all instances of a contract. They are stored in memory, not in storage, and their value persists across function calls. Static variables are used to store data that is common to all instances of a contract, such as a counter that keeps track of the number of times a function has been called.
pragma solidity ^0.8.0;

contract MyContract {
    static uint count;

    function incrementCount() public {
        count++;
    }
}
  • Local variables: Local variables are declared within the scope of a function and are only accessible within that function. They are stored in memory, not in storage, and their value is deleted when the function returns. Local variables are used to store temporary data that is only needed within a specific function.
pragma solidity ^0.8.0;

contract MyContract {
    function add(uint a, uint b) public pure returns (uint) {
        uint result = a + b;
        return result;
    }
}
  • Global Variables: Global variables are pre-defined variables provided by the Solidity compiler. They can be accessed and used inside any function or contract. An example of a global variable is msg which contains information about the current message call.
pragma solidity ^0.8.0;

contract Example {
    uint globalCount;

    function increment() public {
        globalCount++;
    }

    function getCount() public view returns (uint) {
        return globalCount;
    }
}

Rules for declaring Solidity Variable Names

In Solidity, variable names must adhere to the following rules:

  1. Variable names must start with a letter or an underscore (_).
  2. Variable names can only contain letters, numbers, and underscores.
  3. Variable names are case-sensitive.
  4. Variable names cannot be the same as any keywords or built-in types in Solidity, such as “contract”, “address”, or “uint256”.
  5. Variable names can be of any length.
  6. Variable names should be descriptive and meaningful to help other developers understand the intent of the code.
  7. They should be written in camelCase

It’s also good practice to use a naming convention for your variables, such as using a prefix to indicate the type or scope of the variable (e.g., “m_” for member variables, “g_” for global variables, etc.).

Scope of Variables

In Solidity, variables can also have different visibility levels, which determine the accessibility of the variable from outside the contract. The four main types of variable visibility in Solidity are:

  1. Public: Variables declared as public are accessible from any contract or external address. They can be read and written to by anyone.
  2. Internal: Variables declared as internal are only accessible from within the contract in which they are declared. They cannot be accessed by external contracts or addresses.
  3. External: Variables declared as external are not accessible from within the contract but only externally to the smart contract. External functions can be called by other contracts or by transactions.
  4. Private: Variables declared as private are only accessible from within the contract or from derived contracts. They cannot be accessed by external contracts or addresses.

It’s generally considered best practice to use the most restrictive visibility level possible for a variable, as this helps to ensure the security and integrity of the contract. For example, state variables that are used for critical internal logic should be declared as private or internal to prevent them from being tampered with by external actors.

here are some examples of using the different visibility levels in Solidity:

  1. Public:
pragma solidity ^0.8.0;
contract Example {
    uint public count;

    function increment() public {
        count++;
    }
}

In this example, the variable count is declared as public, meaning it can be accessed and modified by any contract or external address.

  1. Internal:
pragma solidity ^0.8.0;
contract Example {
    uint internal count;

    function increment() internal {
        count++;
    }
}

In this example, the variable count is declared as internal, meaning it can only be accessed and modified by functions within the same contract.

  1. External:
pragma solidity ^0.8.0;
contract Example {
    function increment() external {
        count++;
    }
    function getCount() external view returns(uint) {
        return count;
    }
}

In this example, the functions increment and getCount are declared as external, meaning they can only be called by other contracts or by transactions.

  1. Private:
pragma solidity ^0.8.0;
contract Example {
    uint private count;

    function increment() private {
        count++;
    }
}

In this example, the variable count is declared as private, meaning it can only be accessed and modified by functions within the same contract and derived contract.

It’s important to note that, if no visibility level is explicitly specified, the default visibility level is internal, and if no scope level is explicitly specified, the default scope level is storage.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments



0
Would love your thoughts, please comment.x
()
x