"To Do List Contract" Example Version 2

In this example im making use of a mapping of bytes. This works similar to creating an array.

A mapping is used to structure value types, such as booleans, integers, addresses, and structs. It consists of two main parts: a _KeyType and a _ValueType; they appear in the following syntax:
mapping (_KeyType => _ValueType) mapName


pragma solidity ^0.4.0;

contract ToDoAreshFlexible{
    //Declare a struct similar to C++ holding details of a single ToDo Item
    struct Item{
        string task;
        uint priority;
    }
    
    //Create a mapping array
    mapping(bytes32 => Item) ToDoList;
    
    //this is a constructor, notice it has the same name as the Contract name
    function ToDoAreshFlexible() public {
    }
    
    //Adds an item to the list using PUSH
    function AddToDo(string _task, uint _priority) public
    {
        //sha3" has been deprecated in favour of "keccak256"
        bytes32 hash = keccak256(_task);
        ToDoList[hash] = Item(_task,_priority);
    }
    
    //returns a single item given an index since it requires more gas to iterate thru
    //returning multiple Items, both task and priority with a single call 
    function getToDoList(bytes32 _taskHash) public view returns(string, uint ){
        return (ToDoList[_taskHash].task, ToDoList[_taskHash].priority);
    }
}

In the screenshot ive made the following changes to pass a string compared to a byte



function getToDoList(string _task) public view returns(string, uint ){
        bytes32 hash = keccak256(_task);
        return (ToDoList[hash].task, ToDoList[hash].priority);
    }

Updated 11/1/2017 Hash function of a struct

Comments

Popular posts from this blog

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Crypto-Currency "Coin Contract" Example

Solidity Hash of Structs (Testing hash uniqueness)