TypeError: Expression has to be an lvalue

Hi.
I’m trying to do this:

function deposit(uint _amount) public payable returns(uint) {
        return address(this).balance += _amount;
    }

But I get: TypeError: Expression has to be an lvalue
Why?

1 Like

This, however, works:

function deposit(uint _amount) public payable returns(uint) {
        uint b = address(this).balance;
        b += _amount;
        return b;
    }
1 Like

Hey @LaszloD, hope you are well.

The problem is that you should never return a complex operation.

return address(this).balance += _amount;

First of all, you dont need to specify address(this).balance to receive funds directly to the contract. As long as a function is payable and you do not specify what to do with msg.value, all funds wil directly go into the contract internal balance.

    //Empty function, funds will be stored in contract.
    function deposit() public payable {

    }

You could have a getContractBalance function that return address(this).balance instead.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

function deposit() public payable returns(uint){
address(this).balance += msg.value;
}
I have similar problem
and i have the same error message

How to write the function to be working?