1

The below contract deploys fine but the method cannot be executed. I just want to assign a long member number to each member like 12345 and maybe look through the array with that ID. Is this possible?

contract MembersMODDED {

    mapping (address => uint) public memberId;
    Member[] public members; 

    struct Member {  
        string Name;
        string SurName;
        address MemberAccountNo;
        bool IsActive;
        int AccountType;
        uint MemberNo;
        uint memberSince;
    }

    function Members( ) { 

    }

    function changeMembership(address memberAccountNo, bool isActive, string name, string surName,int accountType,uint memberNo) {
        uint id;
        if (memberId[memberAccountNo] == 0) {
            memberId[memberAccountNo] = memberNo;
            id = memberNo;
            members[id] = Member({MemberAccountNo: memberAccountNo, IsActive: isActive, memberSince: now, Name: name, SurName: surName, AccountType: accountType, MemberNo: memberNo});
        } else {
            id = memberId[memberAccountNo];
            Member m = members[id];
            m.IsActive = isActive;
            m.Name = name;
           m.SurName = surName;
        }  
    } 
}

1 Answer 1

2

This is because you're trying to suddenly access a high array index in members when assigining members[id]. You can fix this by switching from an array to a mapping. Below is the fixed code:

contract MembersMODDED {

    mapping (address => uint) public memberId;
    mapping (uint => Member) public members; 

    struct Member {  
        string Name;
        string SurName;
        address MemberAccountNo;
        bool IsActive;
        int AccountType;
        uint MemberNo;
        uint memberSince;
    }

    function MembersMODDED( ) { 
        memberId[12345] = 0;
    }

    function changeMembership(address memberAccountNo, bool isActive, string name, 
    string surName,int accountType,uint memberNo) {
        uint id;
        if (memberId[memberAccountNo] == 0) {
            memberId[memberAccountNo] = memberNo;
            id = memberNo;
            members[id] = Member({MemberAccountNo: memberAccountNo, IsActive: isActive, memberSince: now, Name: name, SurName: surName, AccountType: accountType, MemberNo: memberNo});
        } else {
            id = memberId[memberAccountNo];
            Member m = members[id];
            m.IsActive = isActive;
            m.Name = name;
            m.SurName = surName;
        }  
    }
}
0

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.