"Polymorphism" and Interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dippykdog@gmail.com

    #1

    "Polymorphism" and Interfaces

    I have an IRange interface defined as...
    Public Interface IRange
    Property Min() As Object
    Property Max() As Object
    End Interface
    ....and I want to implement that in different kinds of range objects
    like IntegerRange, StringRange, WeightRange, CurrencyRange, etc.
    But I can't implement the interface using more specific types like
    this
    Public Class IntegerRange
    Implements IRange
    Private _max As Integer
    Public Property Max() As Integer Implements IRange.Max
    Get
    Return _max
    End Get
    Set(ByVal value As Integer)
    _max = value
    End Set
    End Property
    ....
    The error is of course that 'Max' cannot implement 'Max' because there
    is no matching property on interface 'IRange'.
    I guess part of me feels like this should be allowed since Integers
    and anything else I want to make a range object out of are Objects.

    In the end I was wanting collections of type-specific range objects
    that I could sort, perhaps by implementing IComparable, and display in
    a windows control. Should I be trying some other path?
    Thanks.

  • Tom John

    #2
    Re: "Polymorph ism" and Interfaces

    How about using generics:

    Public Interface IRange(Of t)

    Property Max() As t
    Property Min() As t

    End Interface

    Public MustInherit Class RangeBase(Of t)
    Implements IRange(Of t)

    Private _max As t
    Public Property Max() As t Implements IRange(Of t).Max
    Get
    Return _max
    End Get
    Set(ByVal value As t)
    _max = value
    End Set
    End Property

    Private _min As t
    Public Property Min() As t Implements IRange(Of t).Min
    Get
    Return _min
    End Get
    Set(ByVal value As t)
    _min = value
    End Set
    End Property
    End Class

    Public Class IntegerRange
    Inherits RangeBase(Of Integer)

    End Class

    Public Class StringRange
    Inherits RangeBase(Of String)

    End Class

    Hope this helps

    Tom

    -----Original Message-----
    From: dippykdog@gmail .com [mailto:dippykdo g@gmail.com]
    Posted At: 04 July 2007 22:15
    Posted To: microsoft.publi c.dotnet.langua ges.vb
    Conversation: "Polymorphi sm" and Interfaces
    Subject: "Polymorphi sm" and Interfaces

    I have an IRange interface defined as...
    Public Interface IRange
    Property Min() As Object
    Property Max() As Object
    End Interface
    ....and I want to implement that in different kinds of range objects
    like IntegerRange, StringRange, WeightRange, CurrencyRange, etc.
    But I can't implement the interface using more specific types like
    this
    Public Class IntegerRange
    Implements IRange
    Private _max As Integer
    Public Property Max() As Integer Implements IRange.Max
    Get
    Return _max
    End Get
    Set(ByVal value As Integer)
    _max = value
    End Set
    End Property
    ....
    The error is of course that 'Max' cannot implement 'Max' because there
    is no matching property on interface 'IRange'.
    I guess part of me feels like this should be allowed since Integers
    and anything else I want to make a range object out of are Objects.

    In the end I was wanting collections of type-specific range objects
    that I could sort, perhaps by implementing IComparable, and display in
    a windows control. Should I be trying some other path?
    Thanks.

    Comment

    • dippykdog@gmail.com

      #3
      Re: "Polymorph ism" and Interfaces

      On Jul 4, 4:28 pm, "Tom John" <tdj...@nospam. nospamwrote:
      How about using generics:
      >
      Public Interface IRange(Of t)
      >
      Property Max() As t
      Property Min() As t
      >
      End Interface
      >
      Public MustInherit Class RangeBase(Of t)
      Implements IRange(Of t)
      >
      Private _max As t
      Public Property Max() As t Implements IRange(Of t).Max
      Get
      Return _max
      End Get
      Set(ByVal value As t)
      _max = value
      End Set
      End Property
      >
      Private _min As t
      Public Property Min() As t Implements IRange(Of t).Min
      Get
      Return _min
      End Get
      Set(ByVal value As t)
      _min = value
      End Set
      End Property
      End Class
      >
      Public Class IntegerRange
      Inherits RangeBase(Of Integer)
      >
      End Class
      >
      Public Class StringRange
      Inherits RangeBase(Of String)
      >
      End Class
      >
      Hope this helps
      >
      Tom
      >
      -----Original Message-----
      From: dippyk...@gmail .com [mailto:dippyk.. .@gmail.com]
      >
      Posted At: 04 July 2007 22:15
      Posted To: microsoft.publi c.dotnet.langua ges.vb
      Conversation: "Polymorphi sm" and Interfaces
      Subject: "Polymorphi sm" and Interfaces
      >
      I have an IRange interface defined as...
      Public Interface IRange
      Property Min() As Object
      Property Max() As Object
      End Interface
      ...and I want to implement that in different kinds of range objects
      like IntegerRange, StringRange, WeightRange, CurrencyRange, etc.
      But I can't implement the interface using more specific types like
      this
      Public Class IntegerRange
      Implements IRange
      Private _max As Integer
      Public Property Max() As Integer Implements IRange.Max
      Get
      Return _max
      End Get
      Set(ByVal value As Integer)
      _max = value
      End Set
      End Property
      ...
      The error is of course that 'Max' cannot implement 'Max' because there
      is no matching property on interface 'IRange'.
      I guess part of me feels like this should be allowed since Integers
      and anything else I want to make a range object out of are Objects.
      >
      In the end I was wanting collections of type-specific range objects
      that I could sort, perhaps by implementing IComparable, and display in
      a windows control. Should I be trying some other path?
      Thanks.
      Hey, I've got it. I think I can use generics. j/k! Thanks a heap, Tom;
      it's perfect!

      Comment

      • =?Utf-8?B?VG9tIEpvaG4=?=

        #4
        Re: &quot;Polymorph ism&quot; and Interfaces


        "dippykdog@gmai l.com" wrote:
        Hey, I've got it. I think I can use generics. j/k! Thanks a heap, Tom;
        it's perfect!
        Good stuff! Generics are pretty cool, more than just strongly typed
        collections!

        Cheers

        Tom

        Comment

        Working...