9

What is the difference between this.balance VS address(this).balance in solidity contracts?

10

this represents a "contract instance" object for the current contract. The balance function is part of the "address" object. You used to be able to do this.balance because solidity would allow implicit access to the address functions through the contract instance object.

However, as of Solidity 0.4.22, the recommended practice is to use address(this).balance which explicitly converts this (the current contract instance) to an address object.

In Solidity 0.5.0, this.balance will be explicitly forbidden, and you will be required to explicitly convert to the address object before you can access it's functions.

You may find code using the older syntax due to historical practices, but you should use the modern syntax for any new contracts that you write.

0

I have to use this, but im on the Energi chain

Solidity 0.7.5

return (payable(address(this))).balance;

3
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Ismael Nov 23 '20 at 20:08
  • 1
    Yes, thats the problem, I would have liked to comment, but i seem to be stuck at 11 😷 Anyway, maybe it helps someone with the same problem, because i needed 1 hour googling and stumbled across this question. – Kim Ilyong Nov 24 '20 at 9:05
  • Answer to newer questions to get more reputation. Eventually a moderator will move your answer to the comments section. – Ismael Nov 24 '20 at 20:47

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.