Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity^0.4.0;
- //Create a contract named DogContract
- contract DogContract{
- //Create a struct named Dog with a string name
- //and an unassigned integer age.
- struct Dog {
- string name;
- uint age;
- }
- //Dog is it is a struct and thus can be called as an array,
- //The Dog Array is named dogs.
- Dog[] dogs;
- // Keep Track of the owner by using a mapping structure to have key point to an
- //unassigned integer for the index position in the array.
- mapping(address => uint) ownerToDog;
- //Setter function to add a dog. The input parameters are with a string name
- //and an unassigned integer age
- function addDog(string _name, uint _age){
- //create owner variable of type address equat to the transactor.
- address owner = msg.sender;
- // Push input for the transactor into the dogs array.
- //This will return the id of the dog in the list (returns the index possition in the array)
- // therefore we stroe it as an unsigned integer named id
- uint id = dogs.push(Dog(_name, _age));
- ownerToDog[owner]=id;
- }
- function getDog() returns (string){
- address owner = msg.sender;
- uint id = ownerToDog[owner];
- return dogs[id - 1].name;
- }
- }
Add Comment
Please, Sign In to add comment