开发者

Why would the VB.NET compiler think an interface isn't implemented when it is?

开发者 https://www.devze.com 2022-12-26 13:07 出处:网络
Update I don\'t think I was clear enough when I originally posted this quesion. Take a look at these screenshots.

Update

I don't think I was clear enough when I originally posted this quesion.

Take a look at these screenshots.

Why would the VB.NET compiler think an interface isn't implemented when it is?

(Link to bigger screenshot here)

Notice the portions I've boxed in red. The class displayed here does implement INotifyPropertyChanged, but the VB compiler seems to think that the PropertyChanged event as declared does not match the signature of INotifyPropertyChanged.PropertyChanged.

Why would the VB.NET compiler think an interface isn't implemented when it is?

(Link to bigger screenshot here)

Here I've selected the offending line of code. Between this and the next screenshot I literally just cut and paste the exact same line back into the file (i.e., I hit Ctrl + X followed by Ctrl + V).

Why would the VB.NET compiler think an interface isn't implemented when it is?

(Link to bigger screenshot here)

Now behold! After cutting and pasting the line back in, the error goes away.

What is going on here?


Original question

I have this happen sometimes, particularly with the INotifyPropertyChanged interface in my experience but I have no idea if the problem is limited to that single interface (which would seem bizarre) or not.

Let's say I have some code set up like this. There's an interface with a single event. A class implements that interface. It includes the event.

Public Interface INotifyPropertyChanged
    Event PropertyChanged As PropertyChangedEventHandler
End Interface

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged _
    (ByVal sender As Object, ByVal e As PropertyChangedEventArgs) _
        Implements INotifyPropertyChanged.PropertyChanged

    ' more code below '
End Class

Every now and then, when I build my project, the compiler will suddenly start acting like the above code is broken. It will report that the Person class does not implement INotifyPropertyChanged because it doesn't have a PropertyChanged event; or it will say the PropertyChanged event can't implement INotifyPropertyChanged.PropertyChang开发者_StackOverflow社区ed because their signatures don't match.

This is weird enough as it is, but here's the weirdest part: if I just cut out the line starting with Event PropertyChanged and then paste it back in, the error goes away. The project builds.

Does anybody have any clue what could be going on here?


The code works fine for me (Visual Studio 2008), you must be encountering some bug.

Anyhow, you can also implement it this way:

Public Class Person
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

End Class


You need to mark the event Public

0

精彩评论

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