0

how to put struct "s1" values in another variable.

pragma solidity ^0.5.1;

contract MyContract{
    struct student{
        int RollNo;
        string Name;
    }

    student public s1=student({RollNo:1, Name:"Test Bunny"});

    int public disint; string public disname;
    disint=s1.RollNo;
    disname=s1.Name;
}

2

You cannot assign it to a variable directly, you has to do it in a function or constructor
Corrected Code: I had moved assignment operation to constructor

pragma solidity ^0.5.1;

contract MyContract{
    struct student{
        int RollNo;
        string Name;
    }

    student public s1=student({RollNo:1, Name:"Test Bunny"});

    int public disint; string public disname;
    constructor() public {
        disint=s1.RollNo;
        disname=s1.Name;    
    }
}
| improve this answer | |
  • okay, it works thx. – Akash Vaid Oct 9 '19 at 10:38
  • 1
    you can optimize it by assigning it when declaring int public disint = s1.RollNo; string public disname = s1.Name; – Iftifar Taz Oct 9 '19 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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