Tengo 2 contratos:
contract Student{
uint ID;
struct stu{
string name;
uint age;
bool tookTest;
}
mapping(uint => stu) StudentNames;
function Student(string _name,uint _age) {
//ID is incremented
stu s = StudentNames[ID];
s.name = _name;
s.age = _age;
s.tookTest = false;
}
}
contract ClassRoom {
address studentAddr;
Student student;
function ClassRoom(address addr) {
studentAddr = addr;
student = Student(addr);
}
//some function that performs a check on student obj and updates the tookTest status to true
}
No quiero heredar ningún contrato. Quiero acceder al objeto de la estructura student
del contrato Student
desde el contrato ClassRoom
. ¿Como va esto?
En el constructor de Student, la asignación de nombre de estudiante y el ID de uint no se inicializan. Si intentas hacer stu s = studentNames[ID]
, solo obtendrás 0. Quieres algo como lo siguiente:
contract Student{
struct stu{
string name;
uint age;
bool tookTest;
}
mapping(uint => stu) public studentNames;
function addStudent (uint ID, string _name, uint _age) {
studentNames[ID] = stu(_name, _age, false);
}
function updateStudent (uint ID) {
studentNames[ID].tookTest = true;
}
}
Puede acceder al mapeo desde fuera del contrato si lo declara como público, como se indicó anteriormente. Tenga en cuenta que esto solo da acceso de LECTURA. Seguirá necesitando una función en el contrato de estudiante para actualizar el miembro del examen.
p.ej
contract ClassRoom {
address studentAddr;
Student student;
function ClassRoom(address addr) {
studentAddr = addr;
student = Student(addr);
}
//some function that performs a check on student obj and updates the tookTest status to true
function updateTookTest (uint ID) {
student.updateStudent(ID);
}
//if you want to access the public mapping
function readStudentStruct (uint ID) constant returns (string, uint, bool) {
return student.studentNames(ID);
}
}
comeback4you
garen vartanian