0

I have been having problems when wanting to access information from a contract that returns it through a view function. This information is a tuple generated from a struct.

The contract code that generates such information is the following:

enum Grade {
    GARDEN,
    PRIMARY,
    SECONDARY,
    PREPARATORY
}

struct Student {
    uint256 generation;
    Grade grade;
    uint256 timestamp;
}

mapping(uint256 => Student) internal students;

function getStudent(uint256 _studentId) public view returns (Student memory) {
    return students[_studentId];
}

When accessing the information through the same contract via the getStudent function, it returns a tuple, but the problem is how to access said information through another contract. specifically the first element of the tuple (uint256 generation).

First of all, Thanks

1 Answer 1

3

You can access the returned tuple by assigning the returned value of your tuple returning function to a tuple of variables

(uint256 generation, Grade grade, uint256 timestamp) = getStudent(student_address)

this will create the variable generation, grade and timestamp and you can use them normally within their scope. Calling this function from another contract is exactly the same procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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