locked
Defining an interface with restrictions as to what classes can implement it RRS feed

  • Question

  • How do I limit an Interface when defining it? What is the correct technical term for what I am describing? For example, I want MyInterface to only be implemented for objects that implement IList(Of T) and ICollection(Of T).

    Tuesday, November 30, 2010 12:12 PM

Answers

  • If you do it this way, It will be impossible to implement your interface without implementing IList and ICollection

     

    Interface MyInterface(Of T) : Inherits IList, ICollection
      Sub MyMethod()
      Sub MyOtherMethod()
    End Interface
    
    • Proposed as answer by Omie Tuesday, November 30, 2010 3:53 PM
    • Marked as answer by Liliane Teng Wednesday, December 8, 2010 2:31 PM
    Tuesday, November 30, 2010 3:46 PM
  • You should not use this method to restrict the arguments type.

    This does compile without error, but will crash at run time

        Dim a As IList = New Integer() {1, 2, 3}
        a.Add(3)
    
    You will get the same problem if you try to restrict your argument type this way
    • Marked as answer by Liliane Teng Wednesday, December 8, 2010 2:31 PM
    Wednesday, December 1, 2010 12:22 PM

All replies

  • Be aware that an interface is nothing more than a contract too what a class minimal should fulfil.

    Nobody is obliged to use the same member names in a class as long as is told what member of the contract (interface) is meant by a member in a class.

    The only things an Interface describes are the members (and their attributes) which should exist to fulfil that contract.

    In fact it is not even needed that those members do the same.

     


    Success
    Cor
    Tuesday, November 30, 2010 12:27 PM
  • If you do it this way, It will be impossible to implement your interface without implementing IList and ICollection

     

    Interface MyInterface(Of T) : Inherits IList, ICollection
      Sub MyMethod()
      Sub MyOtherMethod()
    End Interface
    
    • Proposed as answer by Omie Tuesday, November 30, 2010 3:53 PM
    • Marked as answer by Liliane Teng Wednesday, December 8, 2010 2:31 PM
    Tuesday, November 30, 2010 3:46 PM
  • I opened this thread with exactly same thought ;-)

    Thanks

    Living on Earth may be expensive, but did you know that it includes a free trip around the sun? Isn't that worth it?
    Tuesday, November 30, 2010 3:54 PM
  • I was thinking you could do something like

    Interface MyInterface(Of T As IList(Of T), ICollection(Of T))

    which would restrict the interface to only classes implementing either IList(Of  T) or ICollection(Of T). What I actually want to do is restrict the Interface to classes that implement one of the generic collections. Would IEnumerable(Of T) be sufficient in that case?

     

     

    Wednesday, December 1, 2010 3:17 AM
  • You should not use this method to restrict the arguments type.

    This does compile without error, but will crash at run time

        Dim a As IList = New Integer() {1, 2, 3}
        a.Add(3)
    
    You will get the same problem if you try to restrict your argument type this way
    • Marked as answer by Liliane Teng Wednesday, December 8, 2010 2:31 PM
    Wednesday, December 1, 2010 12:22 PM