Skip to content

Latest commit

 

History

History
44 lines (28 loc) · 1.77 KB

public_vars.md

File metadata and controls

44 lines (28 loc) · 1.77 KB

Chapter 9: Public Variables

Storage variables can be marked as public during declaration:

publicName: public(String[64])

The compiler automatically creates getter functions for all public storage variables. These getter functions are NOT written in your code, but are generated by the compiler when it compiles it.

For the example above, the compiler will generate a function named publicName that does not take any arguments and returns an String[64], the value of the state variable publicName. So, even if you cannot see any function named publicName, it will be generated when your code is compiled.

Put it to the test

We need a public storage variable that tracks the number of pokemons created in the contract.

  1. Create a public storage variable named totalPokemonCount of type uint256.

  2. Replace the key 0 in the mapping pokemonList in the function _createPokemon with the storage variable totalPokemonCount. Remember to use the self environment variable to access the storage variable from the function.

  3. In the _createPokemon function, increment totalPokemonCount by 1 using the self environment variable. To make the code look clean use the += arithmetic operator as shown below:

    # adds 1 to the parameter passed
    @external
    def addOne(number: uint256):
       # we used += to increment number by 1
       number += 1

** Template **

embedded-code

** Solution **

embedded-code-final

** Previous Chapter Solution **

embedded-code-previous