Question

This might be little bit stupid question but I could not find any work-around or think of any solutions to the following problem...

  public class Example: IExample, INotifyPropertyChanged
  {
    public Example()
    {
    }

    /// Does not works properly... 
    string fooString;
    string IExample.A
    {
        get { return this.fooString; }
        set
        {
            this.fooString= value;
            onPropertyChange("A");
        }
    }

    /// Works just fine
    string fooString;
    public string A
    {
        get { return this.fooString; }
        set
        {
            this.fooString= value;
            onPropertyChange("A");
        }
    }

    PropertyChangedEventHandler propertyChangedHandler;

    private void onPropertyChange(string propertyName)
    {
        if (this.propertyChangedHandler != null)
            propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
    }

    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { this.propertyChangedHandler += value; }
        remove { this.propertyChangedHandler -= value; }
    }

 }

As you can see from the code I have a class Example who implements from INotifyPropertyChanged and my IExample interface who has 1 property A.

Since I am using Explicit interface implementation I am forced to refer to my A through my IExample interface

And that is where my problem is. Since A is coming from IExamle Explicitly when value changes INotifyPropertyChanged does not gets fired...

Which makes sense.

Any thoughts/ideas how to keep Explicit interface implementation and INotifyPropertyChanged and still get the job done?

You might ask why I am obsessed with Explicit interface implementation

1) it's cool [I know horrible reason]
2) its clarity and better code readability [mostly because of this]
3) It forces you to use Interface   

Btw Feel free to critique INotifyPropertyChanged implementation. I feel I can re-implement this is a way so that it can handle Explicit inheritance

Thank you all in advance.

[Edit] Changed "explicit inheritance vs explicit interface implementation" - Like Daniel Corrected me, Added Implicit Inheritance code. Obviously one of the inheritances should be commented out...

Was it helpful?

Solution

As I said in my comment, you must be doing something wrong in the code you didn't show us. Here the event is raised:

var example = new Example();
((INotifyPropertyChanged)example).PropertyChanged += OnAChanged;
((IExample)example).A = "new string";

see http://ideone.com/jMpRG7

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top