"To Do List Contract" With Remix

For this Post, i am using Remix to see the result of the contract.

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
    //returning multiple Items, both task and priority with a single call 
    function getToDoList(uint _index) public returns(string, uint ){
        return (ToDoList[_index].task, ToDoList[_index].priority);
    }
    
    function GetTask(uint _index) constant returns (string)          
    {
        return ToDoList[_index].task;
    }
}

In the above solidity contract i have added a "constant" function.

constant functions execute locally and return a value. Non-constant functions execute on the blockchain and return a transaction hex.
In other words, if you’re trying to get a value out of your contract, you probably want a function that is constant. If you’re trying to update a value in your contract, you do not want a function that is constant.

By running the contract, Make sure you are in the "Run" Tab and "Javascript VM" is selected. Click "Create"




You will now see an instance of the contract created.
To add an item to the list add the following:  "Clear Ref", 1
make sure to include the double quotes for string values and click "AddToDo" once done.

to see the task in the list click "GetTask" after entering index 0.
in my example the Clean ref is in Index 1




Comments

Popular posts from this blog

Crypto-Currency "Coin Contract" Example

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

Solidity Hash of Structs (Testing hash uniqueness)