Non fixed Number of Items for a "To Do List Contract" Example

In this example i will be using an array in storage compared to the Previous Post of a Fixed Number of items in the Array


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;
    }
    
    //An Array of structs similar to C++ (flexible storage) and index start at 0
    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
    {
        ToDoList.push(Item(_task,_priority));
    }
    
    //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?