Skip to main content

LEARN SOLIDITY LESSON 7: Storage & Memory. Internal & External in Solidity

LEARN SOLIDITY LESSON

Storage & Memory in Solidity. Internal & External in Solidity


Storage & Memory in Solidity

Storage refers to variables stored permanently on the blockchain. 
Memory variables are temporary, and are erased between external function calls to your contract. 
Think of it like your computer's hard disk vs RAM.
  Sandwich[] sandwiches;

  function eatSandwich(uint _index) public {

    // So instead, you should declare with the `storage` keyword
    Sandwich storage mySandwich = sandwiches[_index];
    mySandwich.status = "Eaten!";
    // If you just want a copy, you can use `memory`:
    Sandwich memory anotherSandwich = sandwiches[_index + 1];
    anotherSandwich.status = "Eaten!";
    sandwiches[_index + 1] = anotherSandwich;
  
  }

Internal & External in Solidity

Internal is the same as private, except that it's also accessible to contracts that inherit from this contract. 

External is similar to public, except that these functions can ONLY be called outside the contract — they can't be called by other functions inside that contract. 
contract Sandwich {
  uint private sandwichesEaten = 0;

  function eat() internal {
    sandwichesEaten++;
  }
}

contract BLT is Sandwich {
  uint private baconSandwichesEaten = 0;

  function eatWithBacon() public returns (string memory) {
    baconSandwichesEaten++;
    // We can call this here because it's internal
    eat();
  }
}

Comments

Popular posts from this blog

LEARN SOLIDITY LESSON 11: Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY

LEARN SOLIDITY LESSON 11 Payable function. Withdraws Ether & Transfer ETH. Check Balance in SOLIDITY Payable function payable functions are part of what makes Solidity and Ethereum so cool — they are a special type of function that can receive Ether. contract OnlineStore { function buySomething() external payable { // Check to make sure 0.001 ether was sent to the function call: require(msg.value == 0.001 ether); // If so, some logic to transfer the digital item to the caller of the function: transferThing(msg.sender); } } Note: If a function is not marked payable and you try to send Ether to it as above, the function will reject your transaction. Withdraws Ether. Get Balance ETH You can write a function to withdraw Ether from the contract as follows: contract GetPaid is Ownable { function withdraw() external onlyOwner { address payable _owner = address(uint160(owner())); _owner.transfer(address(this).balance); } } It is important to note that you c

LEARN SOLIDITY LESSON 6: Require. Inheritance. Import Solidity

LEARN SOLIDITY LESSON Require Solidity. Inheritance Solidity. Import Solidity Require Solidity For that we use require. require makes it so that the function will throw an error and stop executing if some condition is not true function sayHiToBean(string memory _name) public returns (string memory) { // Compares if _name equals "Bean". Throws an error and exits if no require(keccak256(abi.encodePacked(_name)) == keccak256(abi.encodePacked("Bean"))); // If it's true, proceed with the function: return "Hi!"; } Inheritance Solidity This can be used for logical inheritance (such as with a subclass, a Cat is an Animal). But it can also be used simply for organizing your code by grouping similar logic together into different contracts. contract Cat { function catchphrase() public returns (string memory) { return "So Wow CryptoCat"; } } contract BabyCat is Cat { function anotherCatchphrase() public returns (string memory) { re