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?

Thomas Jay Rush
  • 9,943
  • 4
  • 31
  • 72

2 Answers2

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

Jaime
  • 8,340
  • 1
  • 12
  • 20
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.

Glorfindel
  • 310
  • 1
  • 5
  • 12
Soham Lawar
  • 2,567
  • 14
  • 38