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 / .NET Framework / CLR / July 2006

Tip: Looking for answers? Try searching our database.

Generic comparison.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Burton - 30 Jun 2006 00:30 GMT
I have a class that tries to compare two generic types:

                if (low < high)
Where they are declared as:

               T low = (T)float.Parse(parts[0]);
    T high = (T)float.Parse(parts[1]);
    if (low < high)

Two questions come up.

1) I get an error Cannot convert type 'float' to 'T'. I can't do T.Parse so
I tried the most general type an hope it can be cast to T but I am getting an
error.

2) This is the most baffling I am getting the error: Operator '<' cannot be
applied to operands of type 'T' and 'T'. What "operators" are available to
compare two generic types?

Thank you.

Kevin
Barry Kelly - 30 Jun 2006 01:58 GMT
> I have a class that tries to compare two generic types:
>
[quoted text clipped - 10 lines]
> I tried the most general type an hope it can be cast to T but I am getting an
> error.

What if T is String or System.Windows.Form? You can't cast a floating
point value to a string or a Form, and that's why the compiler won't
accept it. Generics in C# are not like templates in C++: the generic
method definition, taken in isolation, must be type-safe itself. In C++,
the template body isn't fully type-checked until you pass it template
arguments.

If you know that only number types will be used as T, you might try
using the Convert class to perform the conversion, but you'll have to
live with the possibility of runtime errors.

> 2) This is the most baffling I am getting the error: Operator '<' cannot be
> applied to operands of type 'T' and 'T'. What "operators" are available to
> compare two generic types?

You can use Comparer<T>.Compare(T,T) to compare generic types. This will
query the types for IComparable<T> and use that if it exists (it does on
float, for example). Similarly, there is an EqualityComparer<T>.

If you want flexibility, write your method or class so that it accepts
IComparer<T> or IEqualityComparer<T>, and default to Comparer<T> /
EqualityComparer<T> if the user doesn't supply a comparer.

-- Barry

Signature

http://barrkel.blogspot.com/

William Sullivan - 03 Jul 2006 13:55 GMT
Generics constraints are your friend.  Pet my constraint; it won't bite:
public class GenericallyComparable<T> where T: IComparer<T> { ... }

> I have a class that tries to compare two generic types:
>
[quoted text clipped - 18 lines]
>
> Kevin
Barry Kelly - 03 Jul 2006 18:33 GMT
> Generics constraints are your friend.  Pet my constraint; it won't bite:
> public class GenericallyComparable<T> where T: IComparer<T> { ... }

You'll probably want "where T : IComparable<T>" instead. Types generally
don't implement their own comparers.

-- Barry

Signature

http://barrkel.blogspot.com/


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.