In ASP.NET/VB.NET, how do I declare Properties/Events such as IsPostBack/Init on an interface that a user control will implement?

StackOverflow https://stackoverflow.com/questions/10414268

  •  05-06-2021
  •  | 
  •  

Question

I'm having trouble porting some .NET2-era C# to .NET2-era VB.NET. Specifically, I am trying to define an interface that an ASP.NET user control will implement.

(For background, I am trying to re-implement Phil Haack's Model-View-Presenter example from several years ago.)

The C# interface I'm working from defines properties and events (IsPostBack, Load) that are already implemented by the base control.

However, VB.NET is forcing me to explicitly implement these properties/events in the user control (Public Property IsPostBack() As Boolean Implements IView.IsPostBack...). I'd like to just define these in the interface and not have to do anything special in the code-behind of the implementing user control.

I'm assuming that I can do this in VB.NET, I just don't know how. I've spent all sorts of time googling/bing-ing, and haven't come up with the answer.

Anyone have any ideas?

Was it helpful?

Solution

Unfortunately, this is not something you can do in VB.NET. VB.NET only does explicit interface implementation, so you would have to do something like this:

Public Class MyPage
    Inherits Page
    Implements IView

    Public ReadOnly Property IsPostBack() As Boolean Implements IView.IsPostBack
        Get
            Return MyBase.IsPostBack
        End Get
    End Property
End Class

Public Interface IView
    Public ReadOnly Property IsPostBack() As Boolean
End Interface

So you would have to implement all of those interface members again.

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