2

Is it possbible to get the Instance of an item, within the CollectionChanged event?

For Example:

public class Foo
{
    public string Name { get; set; }
    public ObservableCollection<Bar> Bars { get; set; }

    public Foo()
    {
        Bars += HelperFoo.Bars_CollectionChanged;
    }
}

public class Bar
{
    public string Name { get; set; }
}

public static class HelperFoo
{
    public static voic Bars_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        //sender is the collection Foo.Bars
        //can I get the Instance of Foo?
    }
}

(I wouldnt mind using reflection)

If this isnt possible is there a way to get the Instance of object that intializes an other object?

For Example:

public class Foo
{
    public string Name { get; set; }

    public Foo()
    {
        var bar = new Bar(); //yes I know, I could write new Bar(this) and provide an overload for this
    }
}

public class Bar
{
    public string Name { get; set; }

    public Bar()
    {
        //Get the Foo, since the constructor is called within Foo, is this possible?
        //without providing an overload that takes an object, and just change it to `(new Bar(this))`
    }
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • You keep referring to `FooA`, but your code does not have a `FooA`. I assume you mean `Foo`. Your naming makes this hard to follow. `Foo` and `Bar` would make this a bit more readable. – Jon B Apr 30 '14 at 17:21
  • possible duplicate of [How can I find the method that called the current method?](http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method) – Jon B Apr 30 '14 at 17:23
  • Not an exact dupe, but that will allow you to identify who called your constructor. – Jon B Apr 30 '14 at 17:23
  • As @JonB points out your question is very confusing because of your naming. If you are looking for the item being modified/added/removed within the collection you should use NotifyCollectionChangedEventArgs http://msdn.microsoft.com/en-us/library/system.collections.specialized.notifycollectionchangedeventargs_properties(v=vs.110).aspx – FodderZone Apr 30 '14 at 17:46
  • @JonB: thanks for the link, but I would need to get the instance of the object thats calling either related to the collectionchanged or from with a constructor. – Rand Random May 09 '14 at 13:20
  • @FodderZone thanks for the link, but I would need the instance of the item thats holding this collection and not the items getting added/removed/replaced/moved/reseted. – Rand Random May 09 '14 at 13:21

2 Answers2

1

Your code seems rather oddly structured.

If your HelperFoo class needs to do something with Foo, then pass it Foo, and let it subscribe to Bar events itself.

If HelperFoo shouldn't know anything about Bars, then expose an event on Foo instead and subscribe to that. You can raise then event inside Foo when Bars changes.

Have a read up on the Law Of Demeter.

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
  • "If your HelperFoo class needs to do something with Foo, then pass it Foo, and let it subscribe to Bar events itself." ... I have like 500 instances of Foo and all need to do a comon thing when the Foo.Bars collection changes, so I believe its memory saver to have a static collection changed event instead of subrcibing it for every instance or Foo, but sadly I need some information about the Foo's instance to handle the logic in collection changed correctly. Would you still suggest registering the collection changed for every instance of Foo? – Rand Random May 09 '14 at 13:48
  • Do all 500 Foos contain the same Bar collection? Or are there 500 different collections too? If so I hardly think 500 delegate subscriptions are going to matter much. – GazTheDestroyer May 09 '14 at 14:06
  • 500 different collections - items between 0 to max 300 – Rand Random May 09 '14 at 14:22
1

I agree with @Gaz but if you are really wanting to do what you describe then add a Dictionary to your HelperFoo class. Then in your Foo class add this as the creator as follows.

public static class HelperFoo
{
    private static Dictionary<ObservableCollection<Bar>, object> lookupCreators = new Dictionary<ObservableCollection<Bar>, object>();

    public static void AddBarsCreator(ObservableCollection<Bar> bars, object creator)
    {
        lookupCreators.Add(bars, creator);
    }

    public static void Bars_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        ObservableCollection<Bar> bars = (ObservableCollection<Bar>)sender;
        if (lookupCreators.ContainsKey(bars))
        {

        }
    }
}

public class Foo
{
    public ObservableCollection<Bar> Bars { get; set; }

    public Foo()
    {
        Bars = new ObservableCollection<Bar>();
        HelperFoo.AddBarsCreator(Bars, this);
        Bars.CollectionChanged += HelperFoo.Bars_CollectionChanged;
    }
}
FodderZone
  • 863
  • 12
  • 27