1

I'm not an absolute beginner, but this one is seemingly beyond me (or maybe I'm out of energy at the end of the day here :)). What is the following code piece trying to achieve (taken from this SO post)?

public class ObservableStack<T> : Stack<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
  //...

  protected virtual event PropertyChangedEventHandler PropertyChanged;

  //...

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

I need to translate this to VB.NET, which doesn't seem to be happy with the existence of two PropertyChanged events. Which one needs to be removed while still implementing the interface correctly?

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • The second one is [an explicit implementation](http://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation) of the same interface. One case where you do that is, for example, when you're implementing both `IEnumerable` and `System.Collections.IEnumerable`. You can override `GetEnumerator()` twice with two different return types, one of them explicitly. That would be otherwise impossible. The explicit thing allows the compiler to distinguish between calls to the two different methods. I'm curious why that guy did it there. – 15ee8f99-57ff-4f92-890c-b56153 Sep 21 '16 at 16:55
  • @EdPlunkett: But looking at the class declaration, the only interface that requires `PropertyChanged` is `INotifyPropertyChanged`. why would we need to override twice then? – dotNET Sep 21 '16 at 16:58
  • I'm hoping somebody smarter than me will answer this question. – 15ee8f99-57ff-4f92-890c-b56153 Sep 21 '16 at 16:59
  • I see he declares the regular `PropertyChanged` as `protected virtual`, so in effect he's got a backing field with the same name as the public property. One effect this would have is that *unless* his class is explicitly cast to `INotifyPropertyChanged`, `PropertyChanged` isn't accessible. I don't know why that would be desirable. With the "backing field" event being virtual, it does allow a subclass to take complete control of the event, I guess. – 15ee8f99-57ff-4f92-890c-b56153 Sep 21 '16 at 17:04
  • 1
    I just discovered that `ObservableCollection` does it the same way (but not virtual). I hadn't known that. "What Would the Framework Do?" is a valid principle IMO. – 15ee8f99-57ff-4f92-890c-b56153 Sep 21 '16 at 17:09
  • @EdPlunkett: Hmm. How would we translate this to VB.NET then since I can't use two events of the same name there? – dotNET Sep 21 '16 at 17:13
  • 1
    Ohhh right, the actual question, ha ha! Easy, two ways: either make the protected event public and delete the explicit one, or else keep it explicit as shown here: http://stackoverflow.com/questions/1577149/explicit-interface-implementation-in-vb-net – 15ee8f99-57ff-4f92-890c-b56153 Sep 21 '16 at 17:16
  • @EdPlunkett: Thanks Ed. I removed custom event and change protected version to public. Hope this doesn't (in some convoluted way) change the meaning of things. I'll accept your answer if you post it. – dotNET Sep 21 '16 at 17:36

1 Answers1

0

There are two ways to do this in VB. First, and easiest, is to get rid of the explicit implementation of INotifyPropertyChanged. Make the protected event public, and don't use the explicit one with the add/remove blocks.

However, it happens that ObservableCollection<T> implements INotifyPropertyChanged explicitly itself, so there may be some good reason to do that. Ordinarily I do whatever the framework does, because historically their ideas are, on average, better than mine. In this case I don't know why they did it that way, but at the very worst it can't hurt.

And it turns out you can implement interfaces explicitly in VB.NET. In theory. But I tried doing something based on this Lovecraftian example code here, and gave up in horror and despair.

I think you'll be fine just implementing the interface regularly.

Community
  • 1
  • 1