What is the difference between this.balance VS address(this).balance in solidity contracts?
- 1,563
- 3
- 14
- 26
2 Answers
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.
- 8,008
- 4
- 19
- 38
I have to use this, but im on the Energi chain
Solidity 0.7.5
return (payable(address(this))).balance;
- 161
- 1
- 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
-
2Yes, 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 09: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
-
I was stuck for like 2 weeks, apparently (for me at least), it can only work if it is casted to payable. THANKS! – jlee88my Jun 15 '22 at 04:23