I have several master objects. Each ot them has list of slave objects. Each of the slave objects has two fields: field1
and field2
. I need to have access to the fields from the main objects ONLY if the main object, who asked for the field, is not an owner of the slave object.
class SlaveObj()
{
...
private readonly int field1;
private readonly string field2;
...
public int GetField1()
{
// if asker object is not my owner
// return field1
}
}
class MainObj()
{
...
List<SlaveObj> slaves = new List<SlaveObj>();
...
public int GetField1(MainObj other)
{
return other.slaves[0].GetField1();
}
}
First, what I tried, was this. I just tried to check, like in the first answer, what object is the asker. But I have something like Project1.MainObj
for any instance of MainObj. So, I can't recognize whether the asker is the owner or not.
Code after changes (not works as i want)
class SlaveObj()
{
...
private MainObj owner;
private readonly int field1;
private readonly string field2;
...
public int GetField1(MainObj asker)
{
if(asker != owner) return field1;
}
}
class MainObj()
{
...
List<SlaveObj> slaves = new List<SlaveObj>();
...
public int GetField1(MainObj other)
{
return other.slaves[0].GetField1(this);
}
}