实现属性错误VB

时间:2015-08-01 23:46:54

标签: vb.net

我正在使用一个从接口实现的类。 我想从界面实现一个属性,但我收到错误:

  

Class' ListManager'必须实施'财产统计为整数' for interface' IListManager(Of T)'。实施财产必须具有匹配的'ReadOnly'或者' WriteOnly'说明符。

接口:

Namespace Assign_1

    Public Interface IListManager(Of T)

        ''' <summary>
        ''' Return the number of items in the collection m_list
        ''' </summary>
        Property Count As Integer
End Namespace

类别:

Namespace Assign_1
    Public Class ListManager(Of T)
        Implements IListManager(Of T)

        Protected m_list As List(Of T)

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New()
            MyBase.New()
            m_list = New List(Of T)
        End Sub

        ''' <summary>
        ''' Property to count the list
        ''' </summary>
        Public ReadOnly Property Count As Integer
            Get
                Return m_list.Count
            End Get
        End Property
End Namespace

有谁知道这个问题?

2 个答案:

答案 0 :(得分:2)

没有必要将接口定义为通用接口,因为接口只是建立了一个实现了属性和方法的契约。

Public Interface IListManager
    ReadOnly Property Count As Int32

    ' function version
    Function GetCount() As Integer
End Interface

Public Class ListMgr(Of T)
    Implements IListManager

    Private myList As List(Of T)

    Public ReadOnly Property Count As Integer Implements IListManager.Count
        Get
            Return myList.Count
        End Get
    End Property

    Public Function GetCount() As Integer Implements IListManager.GetCount
        Return myList.Count
    End Function

End Class

如果您确实需要接口是通用的,那么只需要进行一些小改动:

Public ReadOnly Property Count As Integer Implements IListManager(Of T).Count

输入Implements IMyInterface后,当您按Enter键时,VS将添加样板代码属性和方法代码。

答案 1 :(得分:0)

这对我有用

Public Interface IListManager(Of T)
    ReadOnly Property count As Integer
End Interface

Public Class ListManager(Of T) : Implements IListManager(Of TabAlignment)
    Protected m_list As List(Of T)

    ''' <summary>
    ''' Constructor
    ''' </summary>
    Public Sub New()
        MyBase.New()
        m_list = New List(Of T)
    End Sub

    Public ReadOnly Property count As Integer Implements IListManager(Of TabAlignment).count
        Get
            Return Me.m_list.Count
        End Get
    End Property
End Class