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.
The same happens with ā1ā, ā2ā, or ā123123123123123ā. Any index will return false, because we didnāt write a value there yet.
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!
- Set the value of myMapping[0] to true, by entering ā0ā next to the āsetValueā button
- Hit the setValue button
- retrieve the myMapping[0] value again, by entering ā0ā next to the āmyMappingā button
- It should return true āØ