I am busy with a project. So I have an array of a struct within another struct. In this case for example array of CattleHealth in Cattle. In my RecordHealth function, I am taking the address of the Cattle struct and adding the current length of its current list to create a unique address. Is this a correct way to do it?
contract WagyuRecordContract
{
address owner;
struct Cattle
{
address RFID;
string Name;
uint256 Weight;
string Gender;
string Colour;
string Breed;
uint Age;
uint DOB;
string Location;
bool Parent;
string SireName;
string DamName;
bool Active;
bool ForSale;
CattleHealth[] HealthRecord;
CattleGrowth[] GrowthRecord;
CattleMovements[] MovementsRecord;
Facility SlaughterDetails;
Meat[] DistributionDetails
}
struct CattleGrowth
{
uint DateRecorded;
uint256 FoodIntake;
uint256 Growth;
}
mapping (address => Cattle) public cattle;
mapping (address=> CattleGrowth) public growth;
modifier Owner()
{
require(msg.sender == owner);
_;
}
function RecordHealth(address rfid, string _bodyCond, string _healthStat, uint256, string _med) Owner public
{
health[rfid+cattle[rfid].HealthRecord.length].DateRecorded = now;
health[rfid+cattle[rfid].HealthRecord.length].BodyCondition = _bodyCond;
health[rfid+cattle[rfid].HealthRecord.length].HealthStatus = _healthStat;
health[rfid+cattle[rfid].HealthRecord.length].Medication = _med;
cattle[rfid].HealthRecord.push(health[rfid+cattle[rfid].HealthRecord.length]);
}
}
Edit: My Buddy used this method below without using a mapping. I don't understand how this works. What is the -1 for? How is it unique to the cattle?
function RecordHealth(address rfid, string _bodyCond, string _healthStat, string _med) Owner public
{
cattle[rfid].HealthRecord.push(now, _bodyCond, _healthStat, _med)-1;
}
rfid+cattle[rfid].HealthRecord.length
even compile? You are trying to addaddress + uint
. Looks like you by the least need to replace it withaddress(uint(rfid)+cattle[rfid].HealthRecord.length)
.health
mapping.