Question

I am trying to implement a derived interface in a class. My interfaces and class are similar to the following. The Namespaces relate to different projects that hold these interfaces and the class:

Namespace ns1

    Public Interface IParent

        Function myFunction() As Double

    End Interface

End ns1

Namespace ns2

    Public Interface IDerived
        Inherits ns1.IParent

        Overloads / Shadows Function myFunction(ByRef myObject as Object) As Double

    End Interface

End ns2

Namespace ns3

    Public Class myClass
        Implements ns2.IDerived

        Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction

        End Function

End ns3

In the derived interface, I am trying to overload the function in a way that when I implement the derived interface, I only have to implement the function as defined therein - as is done with the code above on "myClass". However, I am getting an error saying I have to also implement the function from the parent interface (with the empty argument list). The error exists regardless of my using Overloads or Shadows on the function in the derived interface - both cause the error.

Is there anyway to accomplish what I am trying to do - implement only the derived interface's function in my class - using interfaces? If there is not a way using interfaces, can anyone suggest an alternate way? We really need to use interfaces and are trying to avoid using classes. That said, abstract classes my allow us to do all we need to do with these.

I have read a lot of info on all the topics covered by this question as every concept is pretty basic and well covered in online help. But, I have not found anything that I recognize as a direct solution to my specific issue.

Thanks in advance for any help.

Was it helpful?

Solution

I don't know if this is a typo but you have two distinct methods: one that takes no parameter, and another that takes an object, so the compiler requirement is legitimate.

If this is a typo and that you have only one method, say "myFunction()", then I fear VB.Net does not act like C# by simply hiding the base interface and allowing to only implement the derived one.

But you could easily fix this by forwarding:

Namespace ns1

    Public Interface IParent

        Function myFunction() As Double

    End Interface

End Namespace

Namespace ns2

    Public Interface IDerived
        Inherits ns1.IParent

        Function myFunction() As Double

    End Interface

End Namespace

Namespace ns3

    Public Class Class1
        Implements ns2.IDerived

        Public Function myFunction() As Double Implements ns2.IDerived.myFunction
            Return 42
        End Function

        Private Function myFunction1() As Double Implements ns1.IParent.myFunction
            Return myFunction()
        End Function
    End Class

End Namespace

Module Module1

    Sub Main()
        Dim cp As ns1.IParent = New ns3.Class1
        cp.myFunction()

        Dim cd As ns2.IDerived = New ns3.Class1
        cd.myFunction()
    End Sub

End Module

EDIT: So was not a typo, here is the standard (good/best practice?) fix:

Public Class Class1
    Implements ns2.IDerived

    Public Function myFunction(ByRef obj As Object) As Double Implements ns2.IDerived.myFunction

    End Function

    Public Function myFunction() As Double Implements ns1.IParent.myFunction
        Throw New NotImplementedException("'ns1.IParent.myFunction' has not been implemented because unicorns can't fly!")
    End Function
End Class

OTHER TIPS

I don't believe that what you want to accomplish is possible the way you are trying... As I recall when you inherit an Interface any class that implements your derived Interface is actually being told that it must implement both Interfaces rather allowing the options you have in a full class.

So effectively what you have in myClass is:

Namespace ns3

    Public Class myClass
        Implements ns2.IDerived
        Implements ns1.IParent

        Public Function myFunction(ByRef obj as Object) As Double Implements ns2.IDerived.myFunction

        End Function

End ns3

So inheriting an interface is really just a way to enforce that a class implementing the derived interface must also implement the base interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top