1

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);
    }
}
Community
  • 1
  • 1
murzagurskiy
  • 1,273
  • 1
  • 20
  • 44
  • When you're executing `slaves[0].GetField1()`, `SlaveObj` has no notion from which `MainObj` instance (**if any!**) that method is called. You need to provide a way for a `SlaveObj` to identify its containing `MainObj`, and that is shown in the [question you link to](http://stackoverflow.com/questions/1940165/how-to-access-to-the-parent-object-in-c-sharp) ... through constructor injection, for example. – CodeCaster Feb 01 '16 at 15:40
  • 1
    Can you add an example when `GetField1()` should return `field1` and when `field2` (it's unclear to me what you call owner)? Maybe you don't need `SlaveObj.GetField1()`, simply check for ownership in `MainObj.GetField1()` ? – Sinatr Feb 01 '16 at 15:44
  • @Sinatr i added my piece of code. I need to know the properties of one SlaveObj of foreign Main objects, but did not know its own slaveObj properties. – murzagurskiy Feb 01 '16 at 15:51
  • @gekOn What do you want to return if the main object is not the master of the slave object inside `SlaveObj.GetField1()`? – Frank Bryce Feb 01 '16 at 15:55

2 Answers2

1

My friend, this should work out the way you need. But you gotta add IDs to parent objects.

    internal class SlaveObj
{
    private MainObj owner;
    private readonly int field1;
    private readonly string field2;

    public SlaveObj(MainObj parent)
    {
        this.owner = parent;
    }

    public int GetFieldID(int askerID)
    {
        if (askerID != owner.ID) return field1;
        return 0;
    }
}

class MainObj
{
    public int ID;

    List<SlaveObj> slaves = new List<SlaveObj>();

    public int GetFieldID(MainObj other)
    {
        return other.slaves[0].GetFieldID(this.ID);
    }

    public MainObj(int id)
    {
        this.ID = id;
    }
}

And your previous version did not work out because your main objects are of reference type thich are compared by reference by default. So better use object IDs implement IEqualtyComparer in MainObj:

 class MainObj : IEqualityComparer
Anatolyevich
  • 671
  • 5
  • 14
0

It's easy to fix

class SlaveObj()
{
    MainObj _owner;
    readonly int _field1 = ...;
    readonly string _field2 = ...;

    // you need a way to set owner, e.g. constructor parameter
    public SlaveObj(MainObj owner)
    {
        _owner = owner; // good example why underscore in field name is good
    }

    // return type: object
    // renamed
    // using C# 6.0 features to confuse people
    public object GetFieldX(MainObj asker) => asker != _owner ? _field1 : _field2;
}

class MainObj()
{
    List<SlaveObj> _slaves = new List<SlaveObj>();

    // return first slave field value
    // has nothing to do with instance, therefore static
    // will return null if no slave
    public static object GetFieldX(MainObj owner) => owner?.FirstOrDefault()?.GetFieldX(this);
}

but it's not pretty.

Sinatr
  • 20,892
  • 15
  • 90
  • 319