pragma solidity >=0.5.0 <0.6.0;
contract Courses {
struct Instructor {
string age; //add memory to struct varibles
string fName;
string lName;
}
mapping (address => Instructor) public instructors; //added public
address[] public instructorAccts;
function setInstructor(address _address, uint256 _age, string memory _fName, string memory _lName) public {
Instructor memory instructor = Instructor(_age, _fName, _lName);
instructor[_address] = instructor;
instructor.age = _age;
instructor.fName = _fName;
instructor.lName = _lName;
instructorAccts.push(_address) -1;
}
function getInstructors() view public returns(address[] memory) {
return instructorAccts;
}
function getInstructor(address _address) view public returns (uint, string memory, string memory) {
return (instructors[_address].age, instructors[_address].fName, instructors[_address].lName);
}
function countInstructors() view public returns (uint256) {
return instructorAccts.length;
}
}
Your code had a couple small bugs in it:
struct Instructor {
uint256 age; // wrong type (was string)
string fName;
string lName;
}
...
function setInstructor(address _address, uint256 _age, string memory _fName, string memory _lName) public {
Instructor memory instructor = Instructor(_age, _fName, _lName);
instructors[_address] = instructor; // typo: instructor instead of instructors
Or just:
function setInstructor(address _address, uint256 _age, string memory _fName, string memory _lName) public {
Instructor storage instructor = instructors[_address];
instructor.age = _age;
instructor.fName = _fName;
instructor.lName = _lName;
instructorAccts.push(_address) -1;
}
Explanation: https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089
Steve is right about the wrong type assigned to age
in the struct
. Everything he said, actually, completely agree.
A concern I have is this function is open to anyone and it doesn't check for the possibility of overwriting important information.
This can help: https://medium.com/robhitchens/solidity-crud-epilogue-e563e794fde
Also, the getInstructors()
function returns an array of arbitrary size. This is a scalability concern because it will not always work or will be become too expensive for practical use. https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad (Internally, the array is iterative).
I'm guessing this is experimental/ These are pointers about where you might want to take this.
Hope it helps.