0

I am writing a contact that sets a new owner as per below but cannot understand why the function setNewOwner needs to be a view function. I would have thought assigning a new owner would change the state so would not require a visability function?

mapping (address => uint) balanceAllowance;
address public owner; 

constructor () {
    owner = msg.sender;
}

function setNewOwner (address _address) public view {
    owner == _address;
}

2 Answers 2

1

Because you put comparator operator == there, you should have used =.

More Explanation

Since you used == function checks if owner and _address equals and does not do any change so your editor suggests you to change it to view. If you have put a return there like below

function setNewOwner (address _address) public view {
    return owner == _address;
}

It would return true.

0

I would like to add that you should notice that anyone can call this function and set it’s owner after you’ll switch owner == _address To owner = _address.

Consider using msg.sender

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.