Ich habe 2 Verträge:
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
}
Ich möchte keinen Vertrag erben. student
Ich möchte auf den Vertragsgegenstand Student
von Vertrag zugreifen ClassRoom
. Wie geht man vor?
Im Konstruktor für Student werden die Zuordnung studentNames und die Uint-ID nicht initialisiert. Wenn Sie versuchen, zu tun stu s = studentNames[ID]
, erhalten Sie nur 0. Sie möchten etwas wie das Folgende:
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;
}
}
Sie können von außerhalb des Vertrages auf das Mapping zugreifen, wenn Sie es wie oben als öffentlich deklarieren. Beachten Sie, dass dies nur Lesezugriff gewährt. Sie benötigen weiterhin eine Funktion im Studentenvertrag, um das takeTest-Mitglied zu aktualisieren.
z.B
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