Skip to Content
Courses

Access Mapping Variables

Mappings are accessed like arrays, but there are no index-out-of-bounds-exceptions. If you don’t know what that means, don’t worry, it was a joke for Java Developers.

All possible key/value pairs are already initialized. You can simply access any key and get back ā€œfalseā€, since that’s the default boolean value.

Give it a try?

Enter ā€œ0ā€ next to ā€œmyMappingā€ and hit the button. It will return false.

ethereum-blockchain-developer-image

The same happens with ā€œ1ā€, ā€œ2ā€, or ā€œ123123123123123ā€. Any index will return false, because we didn’t write a value there yet.

ethereum-blockchain-developer-image

Write to a Mapping

If you have a look at our simple function setValue(uint _index), then you will see you will write ā€œtrueā€ to an _index. So, if the _index = 0, then myMapping[0] will be set to true. Again, no need to initialize anything here, it will just assign the value.

Give it a try!

  1. Set the value of myMapping[0] to true, by entering ā€œ0ā€ next to the ā€œsetValueā€ button
  2. Hit the setValue button
  3. retrieve the myMapping[0] value again, by entering ā€œ0ā€ next to the ā€œmyMappingā€ button
  4. It should return true ✨

ethereum-blockchain-developer-image

Last updated on