Solidity – Resolving TypeError: Invalid Type for Argument in Function Call

contract-debuggingcontract-developmentsolidity

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);

Best Answer

The comment says implicit conversion from contract DappTokenSale to address.

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

Related Topic