"Polymorphism" and Interfaces

A

Anon

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.
 
T

Tom John

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: (e-mail address removed) [mailto:[email protected]]
Posted At: 04 July 2007 22:15
Posted To: microsoft.public.dotnet.languages.vb
Conversation: "Polymorphism" and Interfaces
Subject: "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.
 
A

Anon

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: (e-mail address removed) [mailto:[email protected]]

Posted At: 04 July 2007 22:15
Posted To: microsoft.public.dotnet.languages.vb
Conversation: "Polymorphism" and Interfaces
Subject: "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.

Hey, I've got it. I think I can use generics. j/k! Thanks a heap, Tom;
it's perfect!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top