0

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...

myermian
  • 31,823
  • 24
  • 123
  • 215
DMasta
  • 5
  • 1
  • 1
  • 5
  • 4
    Interfaces are *implemented* by classes, not *inherited*. Thus it is called *explicit interface implementation* and not *explicit inheritance*. Finally, your problem has nothing to do with explicit interface implementation. If the event is not raised, that's most likely because of the fact that no-one subscribed to it. Having said that, it actually isn't really clear what your problem is here. Is the event not raised? If so, how do you know? Or is the problem that a bound control isn't being updated? – Daniel Hilgarth Mar 21 '13 at 12:07
  • BTW: Your reasons for using explicit interface implementation seem strange. "1. It's cool" - you already noticed yourself that this isn't a reason. "2. It makes the code more clear and readable" - how so? "3. It forces you to use interface" - and why would you want to force me to use the interface? There are cases when you want to do this, but those are no general cases. – Daniel Hilgarth Mar 21 '13 at 12:11
  • What makes you think the event isn't fired? – Rowland Shaw Mar 21 '13 at 12:12
  • I don't see how explicit is "cool" and I disagree completely with your #2--_MORE_ code is rarely ever more "Readable". All that said @DanielHilgarth is right--nothing to do with the actual issue at hand. – OnResolve Mar 21 '13 at 12:12
  • Daniel - When I change code to Implicit Implementation code works just as expected Event works fine. But as soon as I change it to Explicit program starts working... 1) its personal choice 2) When I see interface I right away know about infrastructure of an object or part of it at least. I think objects are ugly they could be mixture of different things - I much rather see/work with Interface than objects. 3) The idea behind is it enforces me to "program to an interface not an implementation". – DMasta Mar 21 '13 at 12:52
  • Rowland: When I get ride off Explicit Implementation program works just fine. – DMasta Mar 21 '13 at 12:56
  • You're doing something wrong in the code you didn't show. – Henrik Mar 21 '13 at 14:01
  • Try it yourself and you will see what I am talking about. Why Explicit is failing makes perfect sense. I want to know if I can rewrite notification implementation so Explicit will work too... – DMasta Mar 21 '13 at 17:31

1 Answers1

1

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

Henrik
  • 23,186
  • 6
  • 42
  • 92