I am checking if a struct exists in vehicleList and returning true if it exists and false if it does not.

However, the function produces an error and I can't understand why it does not work.

TypeError: Type uint256 is not implicitly convertible to expected type bool. if (bytes(vehicleList[_registrationNo].vehicleRegistrationNo).length =! 0) {

function checkVehicleExists (string memory _registrationNo) public view returns (bool) {
        bool vehicleExists = false;
        if (bytes(vehicleList[_registrationNo].vehicleRegistrationNo).length =! 0) {
            vehicleExists = true;
        return (vehicleExists);

1 Answer 1


The ! should come before the =, so:

if (bytes(vehicleList[_registrationNo].vehicleRegistrationNo).length != 0) {
  // ...

length =! val basically means you're assigning length to !val (not val). If you do that in an if-statement (if (length = !val)), it's the equivalent of writing this:

uint256 length = !val;
if (length) {
  // ...

Both the assignment and if-statement won't work here because you cannot convert from uint256 to bool.

  • Thanks for the help! Damn, that's a really silly mistake by me which I couldn't notice.
    – kahhao39
    Jan 25, 2021 at 18:08
  • No problem! If it helped you out please mark it as answered.
    – Morten
    Jan 25, 2021 at 18:29
  • TypeError: Explicit type conversion not allowed from "uint256" to "bytes memory".
    – KasparTr
    May 9, 2023 at 3:01

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.