0
pragma solidity ^0.4.16;

contract init {
    address[] public userAddresses;
    function addStudent(address _user) public {
       userAddresses.push(_user);
     }

     function sendToken(uint value) payable{
         for (uint i=0; i<userAddresses.length; i++) {
             userAddresses.send(value);
        }
     }

}

i have used both send and transfer but nothing worked out. i have also added "payable" keyword when initialising addresses array but it also shows error.

attached is the error

3

Array userAddresses stored multiple addresses in it. So you can not call send or transfer method with multiple addresses at a time so you have to call like this : userAddresses[i].transfer(value);.

So your code should be like this:

pragma solidity ^0.4.16;

contract init {
    address[] public userAddresses;
    function addStudent(address _user) public {
       userAddresses.push(_user);
     }

     function sendToken(uint value)public payable{
         for (uint i=0; i<userAddresses.length; i++) {
             userAddresses[i].transfer(value);
        }
     }

}

Note: always use transfer method instead of send. it is good practice for improve your code performance.

  • thank you so much to all. How can i miss it? it worked...thanks again to all – Suman Raj Jan 1 at 12:57
1

Change this:

userAddresses.send(value)

To this:

userAddresses[i].send(value)

Or better yet, to this:

userAddresses[i].transfer(value)

And get rid of the payable keyword in the function declaration, as it doesn't seem necessary for this function to receive funds into the contract.

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.