【问题标题】:Class 'ProductRepository' must implement 'Sub Remove(Id As Integer)' for interface 'IProductRepository'类“ProductRepository”必须为接口“IProductRepository”实现“Sub Remove(Id As Integer)”
【发布时间】:2018-07-18 15:36:00
【问题描述】:

我试图将https://docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations 中的 C# 代码转换为 VB .Net。

我已经创建了如下接口 IProductRepository,

Namespace ProductStore.Models

    Interface IProductRepository

        Function GetAll() As IEnumerable(Of Product)

        Function [Get](ByVal id As Integer) As Product

        Function Add(ByVal item As Product) As Product

        Sub Remove(ByVal id As Integer)

        Function Update(ByVal item As Product) As Boolean

    End Interface
End Namespace

然后创建 ProductRepository 类如下,

Namespace ProductStore.Models

    Public Class ProductRepository
        Inherits IProductRepository

        Private products As List(Of Product) = New List(Of Product)()

        Private _nextId As Integer = 1

        Public Sub New()
            Add(New Product With {.Name = "Tomato soup", .Category = "Groceries", .Price = 1.39D})
            Add(New Product With {.Name = "Yo-yo", .Category = "Toys", .Price = 3.75D})
            Add(New Product With {.Name = "Hammer", .Category = "Hardware", .Price = 16.99D})
        End Sub

        Public Function GetAll() As IEnumerable(Of Product)
            Return products
        End Function

        Public Function [Get](ByVal id As Integer) As Product
            Return products.Find(Function(p) p.Id = id)
        End Function

        Public Function Add(ByVal item As Product) As Product
            If item Is Nothing Then
                Throw New ArgumentNullException("item")
            End If

            item.Id = Math.Min(System.Threading.Interlocked.Increment(_nextId), _nextId - 1)
            products.Add(item)
            Return item
        End Function

        Public Sub Remove(ByVal id As Integer)
            products.RemoveAll(Function(p) p.Id = id)
        End Sub

        Public Function Update(ByVal item As Product) As Boolean
            If item Is Nothing Then
                Throw New ArgumentNullException("item")
            End If

            Dim index As Integer = products.FindIndex(Function(p) p.Id = item.Id)
            If index = -1 Then
                Return False
            End If

            products.RemoveAt(index)
            products.Add(item)
            Return True
        End Function
    End Class
End Namespace

但是我收到了 Inherits IProductRepository 行的错误,因为“类只能从其他类继承。”。当我将 Inherits 更改为 Implements 时,如下所示 实现 IProductRepository

我收到以下错误,

类 'ProductRepository' 必须实现 'Function GetAll() As System.Collections.Generic.IEnumerable(Of Product)' 用于接口 'IProductRepository'

对于 IProductRepository 中的其他接口也是如此。你能帮我解决这个问题吗?

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    你的类 ProductRepository 应该实现接口 IProductRepository 像,

    Public Class ProductRepository
            Implements IProductRepository
    

    并且您必须显式使用 Implements 关键字来实现 ProductRepository 类中的接口方法

    Public Function GetAll() As IEnumerable(Of Product) Implements IProductRepository.GetAll
                Return products
    End Function
    

    【讨论】:

    • 并且他们确实需要将顶部的Inherits 转换为Implements,他们表示他们已经尝试过了。如果他们只取问题中的原始代码并将Implements 添加到方法中,他们仍然会出错。
    • @Damien_The_Unbeliever,好的。为了避免混淆,我也添加了类声明。
    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多