Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

IComparable and generics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
teel - 27 Sep 2007 22:45 GMT
Hi there!
I've got a Parameter (any parameter you can imagine) class used to
store a value. It has minValue, maxValue and the current value.
Parameter can be float, bool, integer and should also be enum. It
looks like this:

     public class Parameter<T> where T : IComparable<T>
     {
        public Parameter(T value, T minValue, T maxValue)
        {
           m_Value    = value;
           m_MinValue = minValue;
           m_MaxValue = maxValue;
        }

        public T Value
        {
           get { return m_Value; }
           set
           {
              if (m_MinValue.CompareTo(value) > 0)
                 m_Value = m_MinValue;
              else if (m_MaxValue.CompareTo(value) < 0)
                 m_Value = m_MaxValue;
              else
                 m_Value = value;
           }
        }

        private T m_Value;
        private T m_MinValue;
        private T m_MaxValue;
     }

And it's working perfectly for int and float types. But when I try to
create an object:
Parameter<MyEnum> obj = new Parameter<MyEnum>();
where:
MyEnum : int {A, B, C;}
I get an error:
The type 'MyEnum' must be convertible to 'System.IComparable<MyEnum>'
in order to use it as parameter 'T' in the generic type or method
'Parameter<T>'.

Could you please explain to me why the MyEnum which actually consists
of a
primitive type objects is not IComparable? If the Parameter stores
only int
values it is comparable, the same to float and I guess any numeric
type. What I am doing wrong? What topic should I get familiar with?

Best regards,
teel
teel - 27 Sep 2007 23:07 GMT
Ok, done. T should be IComparable not IComparable<T>

Best regards,
teel
Ben Voigt [C++ MVP] - 27 Sep 2007 23:19 GMT
> Hi there!
> I've got a Parameter (any parameter you can imagine) class used to
[quoted text clipped - 46 lines]
> values it is comparable, the same to float and I guess any numeric
> type. What I am doing wrong? What topic should I get familiar with?

I think that enum types only support the non-generic IComparable interface.
You could also get rid of the IComparable requirement and use
Comparer<T>.DefaultComparer, this will automatically pick the IComparable
implementation if it is available (but it is likely to be quite a bit
slower).

> Best regards,
> teel

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.