Fixed Number of Items for a "To Do List Contract" Example


So in this post, i have a "To Do List" with constant number of items


pragma solidity ^0.4.0;

contract ToDoAreshConstant{
    //Declare a struct similar to C++ holding details of a single ToDo Item
    struct Item{
        string task;
        uint priority;
    }
    uint CurrentItem;
    //An Array of structs similar to C++ with 10 items (reserves storage) and index start at 0
    Item[10] ToDoList;
    
    //this is a constructor, notice it has the same name as the Contract name
    function ToDoAreshConstant() public {
        CurrentItem = 0; //Assign to zero when contract is created.
    }
    
    //Adds an item to the list and increments the position of the index
    function AddToDo(string _task, uint _priority) public
    {
        ToDoList[CurrentItem] = Item(_task,_priority);
        CurrentItem++;
    }
    
    //returns a single item given an index since it requires more gas to iterate thru 
    function getToDoList(uint _index) public returns(string ){
        return ToDoList[_index].task;
    }
}

Comments

Popular posts from this blog

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

Describing the syntax of Solidity using BNF

Ethereum 2.0 the environmentally-conscious blockchain?