3

Good day I've got a struct like this, and I also declare an array to store this struct.

Then in the same contract, I tried to check if the corresponding struct is null or not by checking its length equals to zero.

But the compiler shows me the error msg in if (propertyList[_id].length == 0) throw; of removeProperty function

Member "length" not found or not visible after argument-dependent lookup in struct Property storage ref

But if I create a function

function getPropertiesLength() constant returns(uint){
    return propertyList.length;
}

Then modify my code to if (getPropertiesLength() == 0) throw;

it works just fine. Why? 0.0

My code is listed below. I appreciate any suggestions and opinions :)

Many thanks!!!

contract usingProperty{
    struct Property{
        bytes32 name;
        uint id;
        mapping (address => bool) accessStakeholders;
        uint[] rating;
    }

    Property[] public propertyList;

    function addProperty(bytes32 _name, address[] _accessStakeholders, uint _rating) returns(bool success, uint _id){

        _id = propertyList.length++;

        Property prop = propertyList[_id];
        for (uint i = 0 ; i < _accessStakeholders.length ; i++){
          prop.accessStakeholders[_accessStakeholders[i]] = true;
        }

        prop.name = _name;
        prop.id= _id;
        prop.rating.push(_rating);
    }


    function removeProperty(uint _id){
        if (propertyList[_id].length == 0) throw;
    }

}
2

Ethereum Virtual Machine (EVM) cannot differ between non-inialized and zero data.

Check the struct for a zero value member that should not be zero. What you can do is:

  if (propertyList[_id].id == 0) throw; 

Of course, in this case, id zero must be a special reserved id in your decentralizd application logic.

  • it works ! But I'm wondering why it also works once I replace getPropertiesLength() function with propertyList[_id].length ? – johnhckuo Feb 22 '17 at 11:55
  • The reason is becase with propertyList[_id] you're possibly accessing a Property value in your propertyList mapping and the Property struct doesn't have a length member. getPropertiesLength() works because it executes length on the propertyList mapping itself, that has a length member. – Giovanni Cappellotto Aug 12 '17 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.