-1

I have an interface IMaths which contains a single function ADD as follows. The name of this project is MyInterface which also becomes the default namespace. The code is as follows.

Class InterfaceHolder
    Interface IMaths
        Function ADD(ByVal a As Integer, ByVal b As Integer) As Integer
    End Interface
End Class

The other project is titled MyMathsLibrary (also a class library) which references MyInterface and ideally I would like to write something like the following:

Public Class MathOperations 
    Implements MyInterface.IMaths  'Getting error
End Class

But its not working. The error message is

"Type Imaths is not defined"

Can someone pls tell me how to create a class library with an interface? And how can another class library reference that and implement the interface in the way shown above - that is without creating an object (note that I was not able to declare the Interface as shared)?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Sougata
  • 319
  • 1
  • 10
  • 1
    What exactly is the error? Always tell us the exact error message you receive when asking a question - it's a lot easier than us having to guess or work it out! – ADyson Oct 29 '21 at 10:13
  • And why is the IMaths interface inside a class? That's a bit odd, and I would guess unnecessary - unless you have a very specific reason for doing so. Have you tried `Implements InterfaceHolder.IMaths`, though? or `Implements MyInterface.InterfaceHolder.IMaths`? Since the interface is inside the class, you'll need to reference the class before the compiler will know where to find it. – ADyson Oct 29 '21 at 10:15
  • @ADyson have tried both, that is `Implements InterfaceHolder.IMaths` and `Implements MyInterface.InterfaceHolder.IMaths` but neither works. The error message is : "Type Imaths is not defined" – Sougata Oct 29 '21 at 11:36
  • @ADyson I tried writing the Interface as it is, that the same thing excluding the Class & End Class statements. But got the exact same error. Thats why I put it in a class - but no particular reason behind that. I was just trying alternatives to see if they work or not. – Sougata Oct 29 '21 at 11:38
  • Putting it into a class is only going to make it less accessible, not more. Can you reference other things from the MyInterface namespace in your MyMathsLibrary code without errors, or not? – ADyson Oct 29 '21 at 11:47
  • @ADyson it seems I cant. There seems to be some problem with the referencing. But let me check it thoroughly and I shall come back – Sougata Oct 29 '21 at 12:27
  • "what role does the word OBJECT play in the statement - Shared Function createMathsObject() As Object?" All classes derive from Object, so it can be placed there and anything could be returned. This is generally a bad idea, though! It would be more correct to make it return MathOperations, or possibly put an INTERFACE name there and then you could return any instance of a class that implements that interface. – Idle_Mind Nov 18 '21 at 20:13
  • Don't change the post into a completely different question. It makes the earlier comments and answers nonsensical. If you have a new question, make a new post about it. I've rolled back your change to restore what was there before. If you want to retrieve what you wrote in order to copy it to a new question you can get it from the question history at https://stackoverflow.com/posts/69767164/revisions – ADyson Nov 18 '21 at 22:45
  • @ADyson noted. Have reposted the question. – Sougata Nov 19 '21 at 07:25
  • @Idle_Mind thank you for your comment. pls note that i have reposted the question here: https://stackoverflow.com/questions/70031094/understanding-the-return-type-in-a-function – Sougata Nov 19 '21 at 07:26
  • @Idle_Mind is there a way for me to move your comment to this new thread where I have reposted the question? – Sougata Nov 19 '21 at 07:27

1 Answers1

0

You will get an error until you actually implement the interface.

Interface IMaths
    Function ADD(ByVal a As Integer, ByVal b As Integer) As Integer
End Interface

Public Class MathOperations
    Implements IMaths  'Getting error

    Public Function ADD(a As Integer, b As Integer) As Integer Implements IMaths.ADD
        Throw New NotImplementedException()
    End Function
End Class
Mary
  • 14,926
  • 3
  • 18
  • 27