Reputation: 82186
Question: I'm trying out to convert this here: http://support.microsoft.com/kb/828736 to VB.net
I got it to work in C#, and it should work without problems in VB.net, the only problem is the managed class won't compile, i get this error:
Error Class "ManagedClass" has to implement "Function Add(Number1 As Integer, Number2 As Integer) As Integer" for the ICalculator-Interface
Why? I see one function declared, and one implemented, and that with the same arguments... What's wrong ?
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace TestLibrary2
' Interface declaration.
Public Interface ICalculator
Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer
End Interface
' Interface implementation.
Public Class ManagedClass
Implements ICalculator
Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer
Return Number1 + Number2
End Function
End Class
End Namespace
Upvotes: 1
Views: 372
Reputation: 239704
In VB.Net, you have to be explicit about your interface implementation - this allows more flexibility than C# (because you can name your functions whatever you like), but is a little more work.
Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer Implements ICalculator.Add
Return Number1 + Number2
End Function
As both M.A. Hanin and myself have mentioned - it lets you pick the names, but neither of us mentioned yet why you might want to do this. One example would be if you're implementing two interfaces that both define the same method name. And you want to expose both of these methods as public methods on your class. In C#, you'd have to create at least one "wrapper" function that calls another to achieve this.
Upvotes: 5
Reputation: 25505
you just need to tell it that the class that has a function with exactly the same name and definition is really what you want to handle the interface function.
Public Function Add(ByVal Number1 As Integer, ByVal Number2 As Integer) As Integer Implements ICalculator.Add
Return Number1 + Number2
End Function
Upvotes: 2