Solidity – How to Assign msg.sender to State Variable without Error

solidity

pragma solidity ^0.8.4;

contract Vote {

address[] public Initiator;

function Initiator() public {
Initiator = msg.sender;

}

}

Best Answer

When you want to put an element into an array you can use or: push() method or can specificy the index where this value must be stored like: array[index] = value;.

In your smart contract, you must change your method function name because it has the same name of address[] variable and then you can put the element into array using one of methods that I described you above.

Example:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Vote {
    address[] public Initiator;

    function setInitiator() public { 
        Initiator.push(msg.sender);
    }

}
Related Topic