Interface implementation in C# and VB.NET

后端 未结 3 1775
青春惊慌失措
青春惊慌失措 2020-12-21 23:21

I have an interface defined in C# project:

public interface IForm
{
    bool IsDisposed { get; }
    void Show();
}

I implemen

3条回答
  •  独厮守ぢ
    2020-12-21 23:49

    Use the shadows keyword if you want to override the standard methods of the Form and replace them with the ones defined in your interface otherwise you are required to use a different name as they are treated as two separate methods.

    Public Class Form1
        Inherits Form
        Implements IForm
    
        Public Shadows Property IsDisposed As Boolean Implements IForm.IsDisposed
    
        Public Shadows Sub Show() Implements IForm.Show
            ' replaces original method in Form class
        End Sub
    
    End Class
    

    Alternative:

    Public Class Form2
        Inherits Form
        Implements IForm
    
        Public Property IsDisposed1 As Boolean Implements IForm.IsDisposed
    
        Public Sub Show1() Implements IForm.Show
            Me.Show() ' Original method still exists and is accessible like this
        End Sub
    End Class
    

提交回复
热议问题