0

I was trying to convert the C# code in https://learn.microsoft.com/en-us/aspnet/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations to VB .Net.

I have created interface IProductRepository like below,

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

Then created ProductRepository class as below,

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

But i am getting error for the line Inherits IProductRepository as "Classes can inherit only from other classes.". When I changed Inherits to Implements like below Implements IProductRepository

I am getting following errors,

Class 'ProductRepository' must implement 'Function GetAll() As System.Collections.Generic.IEnumerable(Of Product)' for interface 'IProductRepository'

and same for other interfaces in IProductRepository. Could you please help me on this.

1 Answer 1

1

Your class ProductRepository should implement the interface IProductRepository like,

Public Class ProductRepository
        Implements IProductRepository

and you must explicitly use the Implements keyword to implement the interface methods in the ProductRepository class

Public Function GetAll() As IEnumerable(Of Product) Implements IProductRepository.GetAll
            Return products
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

and they do need to carry out the swap for Inherits to Implements at the top, which they indicated that they've tried. If they take just the original code in the question and add Implements to the methods, they'll still have an error.
@Damien_The_Unbeliever, ok. Just to avoid confusion, I have added that the class declaration too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.