3

i want to count every time that i call the function , i tried to increase the number by every time that I called but it doesn't work , it always gives 1 .

function getdeployedcontracts () public returns (string, uint,uint,uint,uint,uint,address,address) {



     Request storage request= contracts [index];

    return (request.d,
            request.valuev,
            request.r,
            request.t,
            request.n,
            request.r,
            request.recipient);
     index+= 1;

 }
3
  • @smarx ,@ MSwezey after deleting view , i cant see the return variable anymore
    – mesh123456
    Mar 15, 2019 at 19:41
  • 1
    this code is actually valid, as index is a variable instantiated inside of the function and thus does not modify state. Mar 15, 2019 at 19:49
  • The logic behind the code may need to be redone. Mar 15, 2019 at 19:49

3 Answers 3

3

view functions are called via a "call" rather than a transaction and thus cannot change state.

Remove the view modifier, and things should work.

2
  • ,@ MSwezey after deleting view , i cant see the return variable anymore
    – mesh123456
    Mar 15, 2019 at 19:42
  • Transactions don't have return values. If you want to communicate a value back to an external caller, you'll need to emit an event.
    – user19510
    Mar 15, 2019 at 19:53
2

view marks the function as read-only, aka, cannot write state changes.

Remove view, and it should work as expected.

Update - your code does not save state still, below properly increments and save state

    uint index;

    function getdeployedcontracts () public returns (uint result) {
            result = index; // get & return previous value, staring with 0
            index += 1; // increment value
     }

Update #2:

pragma solidity ^0.5.0;


contract U256 {

    struct Request{
        string name;
        uint a;
        uint b;
        uint c;
        uint d;
        uint e;
        address a1;
        address a2;
    }

    Request[] contracts;
    uint index;

    constructor() public {
        contracts.push(Request('first',1,2,3,4,5,address(0),address(0)));
        contracts.push(Request('second',2,3,4,5,6,address(0),address(0)));
    }

    function getdeployedcontracts () public returns (string memory, uint,uint,uint,uint,uint,address,address) {

        Request memory request = contracts[index];

        index += 1;

        return (request.name,
                request.a,
                request.b,
                request.c,
                request.d,
                request.e,
                request.a1,
                request.a2);
    }
}
9
  • @smarx ,@ MSwezey after deleting view , i cant see the return variable anymore
    – mesh123456
    Mar 15, 2019 at 19:42
  • I verified on remix, the return value is there still. But your code needs to be updated. See my updated comment in 1 minute. Mar 15, 2019 at 19:46
  • could i ask which compiler version that u use , i tried with 4.17 and it is still the same
    – mesh123456
    Mar 15, 2019 at 19:53
  • Compiler version doesn't matter in this particular instance. You need to check your transactions decoded output. What tool(s) are you using? Remix? Mar 15, 2019 at 20:52
  • yes remix , u can see the code that i tried . actually i want to view one request in my array like contract [index] , so i want used the idea of counter to show at first , 0 then 1 ,.... . but i cant know how to express my idea , (i edited the code )
    – mesh123456
    Mar 15, 2019 at 21:29
0

put index+= 1 or index.increment() or index = index+1 before return.

The function stop when something return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.