dotNET dotNET - 1 year ago 62 Question

virtual and normal event of the same name in the same class

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
events. Which one needs to be removed while still implementing the interface correctly?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download