0

I wish to return the new index value once the transaction is done.

pragma solidity ^0.4.18;
contract Courses {

    struct Instructor {
        uint age;
        string fName;
        string lName;
    }

    mapping (address => Instructor) instructors;
    address[] public instructorAccts;

    function setInstructor(address _address, uint _age, string _fName, string _lName) public {
        var instructor = instructors[_address];

        instructor.age = _age;
        instructor.fName = _fName;
        instructor.lName = _lName;

        instructorAccts.push(_address) -1;
    }

    function getInstructors() view public returns(address[]) {
        return instructorAccts;
    }

    function getInstructor(address _address) view public returns (uint, string, string) {
        return (instructors[_address].age, instructors[_address].fName, instructors[_address].lName);
    }

    function countInstructors() view public returns (uint) {
        return instructorAccts.length;
    }
}

So in the setInstructor I wish to return the index value like 0,1,2 (Whatever the index value will be there of public key index).

Can anyone help me with this?

1
function setInstructor(address _address, uint _age, string _fName, string _lName) public returns(uint256){
    var instructor = instructors[_address];

    instructor.age = _age;
    instructor.fName = _fName;
    instructor.lName = _lName;

    return instructorAccts.push(_address) -1;
}

Hope this helps

| improve this answer | |
  • Thanks for your answer. But this do not work when we compile on injected web on remix . It will only work on VM. How can I make it work on injected web 3 or on live environment. – shrey bhardwaj Sep 9 '18 at 13:34
  • what do you mean it doesn't work, what error you got? – Jaime Sep 10 '18 at 9:30
  • It should work Shrey – Soham Lawar Sep 10 '18 at 13:21
  • Let me try again. – shrey bhardwaj Sep 14 '18 at 11:36
1

geth is returning transaction hash and remix is returning index. This is a fundamental difference in behavior of geth and remix. The return value of a setInstructor.sendTransaction method is always the hash of the transaction that’s created. Transactions don’t return a contract value to the front end because transactions are not immediately mined and included in the blockchain. To get values from a function you should use solidity events.

Refer to this blog for understanding more about solidity events.

Refer this question to understand why to write return statements in solidity functions.

| improve this answer | |

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