Question

Currently we're using objects and object collections like so:

Public Class childObj Inherits BaseObjClass Implements IInspRevChild  
  ...  
End Class  

Public Class childObj_Collection Inhertis BaseObjCollClass(Of childObj)  
  ...  
End Class  

Public Class parentObj Inherits BaseObjClass Implements IInspRev  
  ...  
  Public Function GetChildren() AS childObj_Collection Implements IInspRev.GetChildrenCollection
    ...  
  End Function  
End Class  

I want to define an Interface with a definition for the GetChildren function.

I was going to try something like this:

Public Interface IInspRev  
  Function GetChildrenCollection() As BaseObjCollClass(Of BaseObjClass )  
End Interface  

But the compiler doesn't like that. Could somebody set me straight?

Was it helpful?

Solution

You're having a circular reference problem. BaseObjClass implements IInspRev, which in turn depends on BaseObjClass. Compiler gets in a "chicken or egg" situation and hence fails.

A better way to define your GetChildrenCollection inside IInspRev method would likely be this:

Readonly Property Childrens() As ICollection(Of IInspRev)

If you don't need the interface to be able to add, remove or replace childs, this will be even better:

Readonly Property Childrens As IEnumerable(Of IInspRev)

Note that I made it a readonly property rather than a method. This way of exposing a collection of child objects is preferred in .NET universe.

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