0

I am fairly new to Solidity and I wanted to understand the Push function better. I am using solidity ver 0.5.12 I wanted to push the Owner address into an array as follows

        address Owner;
        constructor() public
        {   
             Owner = msg.sender;       
        }
        address[] playersAddressList;
        playersAddressList.push(Owner);

Remix is giving me an error stating browser/Lottery.sol:53:32: ParserError: Expected identifier but got '(' playersAddressList.push(Owner); ^

However, when I am trying to push the Owner into an array inside the following function there is no error

  function addPlayer(string memory a, uint b) public payable
    {
        playersAddressList.push(Owner);
    address creator = msg.sender;
    playersAddressList.push(creator);

    balance = balance + msg.value;
    require (msg.value >= 1 ether);

    person memory newPerson;

    newPerson.name = a;
    newPerson.age = b;

    players[creator] = newPerson;

}

I wanted to understand why I can push the Owner's address to an array inside the function but not outside it?

Thanks Rwiju

Rwiju Pal
  • 47
  • 5

1 Answers1

1

Other than declaring state variables and initializing them to values, you cannot have code executed outside of functions.

In fact, even the initializing them to values part is actually "moved silently" into the constructor function by the compiler.

goodvibration
  • 26,003
  • 5
  • 46
  • 86