类 'ProductRepository' 必须实现接口'IProductRepository'的'Sub Remove(Id As Integer)'



我试图在https://learn.microsoft.com/en-us/aspnet/web-api/web-api/overview/older-older-wolder-versions/creating-a---中转换C#代码Web-api-sup-supports-crud-oserations to vb .net。

我已经创建了Interface 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

,但我遇到了该行的错误,因为"类只能从其他类中继承"。当我将继承更改为以下工具 实现IproDuctRepository

我遇到以下错误,

类" productrository"必须实现'getall((为 system.collections.generic.generic.ienumerable(产品('界面' 'iproductrepository'

,对于IproDuctRepository中的其他接口而言相同。您能帮我吗?

您的类 productrepository 应实现接口 iproDuctrePository like,

Public Class ProductRepository
        Implements IProductRepository

您必须明确使用 instrument 关键字来在producttrepository类中实现接口方法

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

最新更新