Search code examples
nullreturnsolidityreturn-type

Solidity return null value


I am new on Solidity and I am struggling on how to return a null value or an empty value.

I have a contract prestataire.sol (with address as one of the parameters) and a contract plateform.sol in which I have a list of prestataires.

I am trying to check if a particular address refers to a prestataire. I would like to return (true, myPrestataire) if the address is correct, and (false, null) if the address is not correct.

But in Solidity, I can't return null, it raises an error. I also tried returning nothing, but that also didn't work.

Here is my function:

function isPrestataire(address checkAdresse) private view returns (bool, prestataire) {
  for (uint i = 0; i < prestataires.length; i++) {
    if (prestataires[i].getAccount() == checkAdresse) return (true, prestataires[i]);
  }
  return (false,  null);//fails
  // return (false); fails also
}

Solution

  • Ok so I figured out my problem.

    //prestataires is a global list of prestataire
    
    function isPrestataire(address checkAdresse) private view returns (bool, prestataire) {
      prestataire p;
      for (uint i = 0; i < prestataires.length; i++) {
        if (prestataires[i].getAccount() == checkAdresse) return (true, prestataires[i]);
      }
      return (false,  p);
    }