Protected Set in VB.Net for a property defined in an interface

这一生的挚爱 提交于 2019-12-05 09:18:56

Yes, you'll have to implement the interface literally. A possible workaround is to republish the property in the class with another name:

Public Class Foo
  Implements IPersistable(Of Integer)
  Private m_Id As Integer

  Public ReadOnly Property Id() As Integer Implements IPersistable(Of Integer).Id
    Get
      Return m_Id
    End Get
  End Property

  Protected Property IdInternal() As Integer
    Get
      Return m_Id
    End Get
    Set(ByVal value As Integer)
      m_Id = value
    End Set
  End Property
End Class

Declare the property Overridable if you intend to override it in derived classes.

It's not currently supported by the language, nor will it in Visual Basic 10 (i.e. the version with Visual Studio 2010). There is a wishlist item for exactly this. Until then, workarounds such as that suggested by nobugz are the only option.

As of Visual Basic 14, your first VB code sample compiles fine.

Interface properties may only be implemented by class properties which match. This is true in both vb.net and C#. What is different in the two languages is that the implicit interface implementation feature of C# will automatically define a read-only or write-only property to implement an interface if a public read-write property of the same name is available.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!