Example :
pragma solidity ^0.4.8;
contract X{
uint amount;
function X() {
amount = amount + msg.value;
}
}
Here Constructor X() is not payable. Should I change it to payable because of using msg.value ?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityExample :
pragma solidity ^0.4.8;
contract X{
uint amount;
function X() {
amount = amount + msg.value;
}
}
Here Constructor X() is not payable. Should I change it to payable because of using msg.value ?
Not mandatory, but as Matthew notes, it would be useless to leave it out of the function that should be receiving ether.
msg.value
gets passed around internally.
Browser Solidity is aware that we don't have a problem in the code below. It's not warning about the presence of msg.value
inside a function that itself isn't payable.
If the logValue()
function is changed to public
, then Browser Solidity warns that there might be an oversight.
Having just tried it in browser-solidity, it will warn you, but it will not stop you.
That said, I'm not sure what use it would have other than calling it through a different, payable function. browser-solidity doesn't have a way to send amounts, it seems. Can anyone test if that works?
msg.value
will always be zero, because any nonzero value will cause an error.ammount
will be zero because it is impossible to add anything to it – Tjaden Hess♦ May 4 '17 at 21:25