1

I am trying to run this code on remix IDE:

pragma solidity ^0.4.24;

contract Aa {

    string public result;    

    function feedData(string _result) public {
        require(_result = "e" , "error");
        _result = result;
    }
}

I keep getting an error: No matching declaration found after argument-dependent lookup. require(_result = "e" , "error");

I looked up the error on stackexchange but the answer doesn't solve this issue. (No matching declaration found after argument-dependent lookup)

When I change the line:

require(_result = "e" , "error");

to => :

require(_result == "e" , "error"); // adding an extra equal sign.

I get the error: Operator == not compatible with types string memory and literal_string "e" require(_result == "e" , "error")

So now my question is: How can I use require() with a string variable or is there a better way to achieve what the code is trying to do?

1 Answer 1

3

There's no built-in equality test for strings (or other arrays), so a common technique is to compare the hashes of the strings instead:

function feedData(string _result) public {
    // Compare hashes
    require(keccak256(bytes(_result)) == keccak256("e"), "error");

    result = _result; // This line was backwards
}

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.