Interfaces

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

So I'm trying to make a generic TreeNode interface in order to simplify converting between a Windows Forms TreeView and an ASP.NET TreeView. I define one of the properties on the interface (Children) as an ICollection, but when I then try to implement the property as a Collection (or ArrayList, or any other class that implements ICollection), it doesn't work. The same is true if I define the interface property as CollectionBase. Am I doing something wrong, or is this just a VB limitation, or something else? I just don't see the point of creating an interface if it can't be defined using base classes or other interfaces... that really limits the usefulness of interfaces as far as I can tell. Any suggestions would be greatly appreciate. Thanks.

Mike
 
Mike,
Can you provide code for what you are trying?

Remember that if you define the method in the Interface with "ICollection"
the implementing method also needs to be ICollection! However you can use
"Explicit Interface Implementation" where you can "overload" the return type
of the implementing method.

Something like:

Public Interface ITreeNode
ReadOnly Property Children() As ICollection
End Interface

Public Class TreeNode
Implements ITreeNode

Private ReadOnly m_children As TreeNodeCollection

Private ReadOnly Property ITreeNode_Children() As
System.Collections.ICollection Implements ITreeNode.Children
Get
Return Me.Children
End Get
End Property

Private ReadOnly Property Children() As TreeNodeCollection
Get
Return m_children
End Get
End Property

End Class

The TreeNode.Children is the type safe collection that matches TreeNode,
while ITreeNode_Children is the version of the method required by the
Interface.

Hope this helps
Jay

Mike Caputo said:
So I'm trying to make a generic TreeNode interface in order to simplify
converting between a Windows Forms TreeView and an ASP.NET TreeView. I
define one of the properties on the interface (Children) as an ICollection,
but when I then try to implement the property as a Collection (or ArrayList,
or any other class that implements ICollection), it doesn't work. The same
is true if I define the interface property as CollectionBase. Am I doing
something wrong, or is this just a VB limitation, or something else? I just
don't see the point of creating an interface if it can't be defined using
base classes or other interfaces... that really limits the usefulness of
interfaces as far as I can tell. Any suggestions would be greatly
appreciate. Thanks.
 
Mike,
I still think the use of derived classes and/or classes
which implement the interface defined on the property
in the interface being implemented
Covariant return types are not supported by VB.NET as they are not supported
by the CLR.

Covariant return types is the ability for a method in a derived class to
return a type that is derived from the return type of the base class's
method, given the rest of the signature is identical.

Eiffel .NET is able to support them within Eiffel code.

There are also Contravariant/covariant parameters, which VB.NET & the CLR
are also not able to support.
should be available directly, but I think this will work for now.
What I showed currently is the only way to do it...

Hope this helps
Jay

Mike said:
Hey thanks a lot Jay, sounds like that will solve my problem. I didn't
realize the property could be implemented with a different access scope
(i.e. Private) from the one defined in the interface. I still think the use
of derived classes and/or classes which implement the interface defined on
the property in the interface being implemented (that was a very confusing
phrase, but I think you know I'm saying) should be available directly, but I
think this will work for now. Thanks again!
 
Back
Top