开发者

Is detaching and reattching events needed when setting a BindingList

I have the following code:

    private BindingList<Box> _boxesToDisplay;
    public BindingList<Box> BoxesToDisplay
    {
        get
        {
            return _boxesToDisplay;
        }
        set 
        {
            // Unhook the old one.开发者_Python百科  Just incase
            _boxesToDisplay.AddingNew -= NewItemAdded;
            // Set the new value
            _boxesToDisplay = value;
            // Hook in the value again.
            _boxesToDisplay.AddingNew += NewItemAdded;
        }
    }

But I got to thinking. It would be best if this was an overload for the assingment operator. Then I got to wondering if they actually did that and I could just replace my code with this:

     public BindingList<Box> BoxesToDisplay { get; set; }

Is true? Would this:

     myClass.BoxesToDisplay = new BindingList<Box>();

still have the AddingNew event set to NewItemAdded() with either definition of BoxesToDisplay?


Would this [...] still have the AddingNew event set to NewItemAdded() with either definition of BoxesToDisplay?

No, you are totally replacing _boxesToDisplay reference. There is no mechanism to transplant eventhandlers.

All I can say is that with the Auto-Imp property you wouldn't need the -= as much as there is no chance that something else will still have access to the old instance. But you would still have to attach to the new one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜