13

I am unable to use the 'this' Keyword in below code.

    function buyTokens(uint256 _numberOfTokens) public payable {
    require(msg.value == multiply(_numberOfTokens, tokenPrice));
    require(tokenContract.balanceOf(this) >= _numberOfTokens);(issue is here)
    require(tokenContract.transfer(msg.sender, _numberOfTokens));

giving below error.

TypeError: Invalid type for argument in function call. Invalid 
  implicit conversion from contract DappTokenSale to address requested.
    require(tokenContract.balanceOf(this) >= _numberOfTokens);
1
  • Replying to @goodvibration Thanks for the tip! The comment says implicit conversion from contract DappTokenSale to address. So just use an explicit conversion instead - change this to address(this). Fully written out explicit conversion would be in your contract: require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);
    – Scott
    Commented May 20, 2021 at 16:43

1 Answer 1

20

The comment says implicit conversion from contract DappTokenSale to address.

So just use an explicit conversion instead - change this to address(this).

1
  • Oh my bad its so silly question. Thanks @goodvibration for that. Commented Jan 1, 2019 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.