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;
}
}
Best Answer
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:
Of course, in this case, id zero must be a special reserved id in your decentralizd application logic.