By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,625 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,625 IT Pros & Developers. It's quick & easy.

"Polymorphism" and Interfaces

P: n/a
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.

Jul 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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: di*******@gmail.com [mailto:di*******@gmail.com]
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.

Jul 4 '07 #2

P: n/a
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.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!

Jul 4 '07 #3

P: n/a

"di*******@gmail.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
Jul 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.