Interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Caputo

    Interfaces

    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
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Interfaces

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

    Remember that if you define the method in the Interface with "ICollectio n"
    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 TreeNodeCollect ion

    Private ReadOnly Property ITreeNode_Child ren() As
    System.Collecti ons.ICollection Implements ITreeNode.Child ren
    Get
    Return Me.Children
    End Get
    End Property

    Private ReadOnly Property Children() As TreeNodeCollect ion
    Get
    Return m_children
    End Get
    End Property

    End Class

    The TreeNode.Childr en is the type safe collection that matches TreeNode,
    while ITreeNode_Child ren is the version of the method required by the
    Interface.

    Hope this helps
    Jay

    "Mike Caputo" <MikeCaputo@dis cussions.micros oft.com> wrote in message
    news:61FBE45D-7725-4E3F-BA77-AB6149443F26@mi crosoft.com...[color=blue]
    > So I'm trying to make a generic TreeNode interface in order to simplify[/color]
    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.[color=blue]
    >
    > Mike[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Interfaces

      Mike,[color=blue]
      > I still think the use of derived classes and/or classes
      > which implement the interface defined on the property
      > in the interface being implemented[/color]
      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.
      [color=blue]
      > should be available directly, but I think this will work for now.[/color]
      What I showed currently is the only way to do it...

      Hope this helps
      Jay

      "Mike" <Mike@discussio ns.microsoft.co m> wrote in message
      news:DC98C7DB-C1C1-42F5-9FB1-62D0C8028EFC@mi crosoft.com...[color=blue]
      > Hey thanks a lot Jay, sounds like that will solve my problem. I didn't[/color]
      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![color=blue]
      >
      > Mike
      >
      > "Jay B. Harlow [MVP - Outlook]" wrote:
      >[color=green]
      > > Mike,
      > > Can you provide code for what you are trying?
      > >
      > > Remember that if you define the method in the Interface with[/color][/color]
      "ICollectio n"[color=blue][color=green]
      > > the implementing method also needs to be ICollection! However you can[/color][/color]
      use[color=blue][color=green]
      > > "Explicit Interface Implementation" where you can "overload" the return[/color][/color]
      type[color=blue][color=green]
      > > 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 TreeNodeCollect ion
      > >
      > > Private ReadOnly Property ITreeNode_Child ren() As
      > > System.Collecti ons.ICollection Implements ITreeNode.Child ren
      > > Get
      > > Return Me.Children
      > > End Get
      > > End Property
      > >
      > > Private ReadOnly Property Children() As TreeNodeCollect ion
      > > Get
      > > Return m_children
      > > End Get
      > > End Property
      > >
      > > End Class
      > >
      > > The TreeNode.Childr en is the type safe collection that matches TreeNode,
      > > while ITreeNode_Child ren is the version of the method required by the
      > > Interface.
      > >
      > > Hope this helps
      > > Jay
      > >
      > > "Mike Caputo" <MikeCaputo@dis cussions.micros oft.com> wrote in message
      > > news:61FBE45D-7725-4E3F-BA77-AB6149443F26@mi crosoft.com...[color=darkred]
      > > > So I'm trying to make a generic TreeNode interface in order to[/color][/color][/color]
      simplify[color=blue][color=green]
      > > converting between a Windows Forms TreeView and an ASP.NET TreeView. I
      > > define one of the properties on the interface (Children) as an[/color][/color]
      ICollection,[color=blue][color=green]
      > > but when I then try to implement the property as a Collection (or[/color][/color]
      ArrayList,[color=blue][color=green]
      > > or any other class that implements ICollection), it doesn't work. The[/color][/color]
      same[color=blue][color=green]
      > > is true if I define the interface property as CollectionBase. Am I[/color][/color]
      doing[color=blue][color=green]
      > > something wrong, or is this just a VB limitation, or something else? I[/color][/color]
      just[color=blue][color=green]
      > > don't see the point of creating an interface if it can't be defined[/color][/color]
      using[color=blue][color=green]
      > > 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.[color=darkred]
      > > >
      > > > Mike[/color]
      > >
      > >
      > >[/color][/color]


      Comment

      Working...