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# / March 2008

Tip: Looking for answers? Try searching our database.

Please help with generics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James - 08 Mar 2008 23:30 GMT
Hi there,

I've have a class called Edge, with the following signature:
class Edge : IComparable{
...
...
...

}

And another class called CompareEdge, with the following signature:
class CompareEdge<T> : IComparer<T> where T : IComparable<T>
{
...
...
...

}

However, In my test class, the following statement is not compiling:

CompareEdge<Edge> comparer = new CompareEdge<Edge>();

I'm getting the following error:
"The type 'MetroNetwork.Edge' cannot be used as type parameter 'T' in the
generic type or method 'MetroNetwork.CompareEdge<T>'. There is no implicit
reference conversion from 'MetroNetwork.Edge' to
'System.IComparable<MetroNetwork.Edge>'."

Can someone please educate me as to why this is NOT compiling and how to fix
it?

Many thanks,
J
Jon Skeet [C# MVP] - 08 Mar 2008 23:41 GMT
> I've have a class called Edge, with the following signature:
> class Edge : IComparable{
[quoted text clipped - 3 lines]
>
> }

Okay. So there you're implementing the *non-generic* interface.

> And another class called CompareEdge, with the following signature:
> class CompareEdge<T> : IComparer<T> where T : IComparable<T>

And there you're demanding that the type parameter T is only used where
T implements the generic interface IComparable<T>.

> However, In my test class, the following statement is not compiling:
>
[quoted text clipped - 8 lines]
> Can someone please educate me as to why this is NOT compiling and how to fix
> it?

IComparable<T> and IComparable are separate interfaces - they might as
well be IFoo<T> and IBar as far as the compiler is concerned. You need
to change the Edge declaration to:

class Edge : IComparable<Edge>
{
...
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Marc Gravell - 08 Mar 2008 23:42 GMT
Edge must be : IComparable<Edge> in order to meet the contraint in
ComparerEdge<Edge>

Actually, I wonder if you can lose ComparerEdge<T> completely, and
just use the regular Comparer<Edge>.Default?

Marc
James - 09 Mar 2008 00:01 GMT
Jon and Marc, it worked perfectly.  Many thanks.

One quick question, before I changed my Edge class declaration to "class
Edge : IComparable<Edge>",
The CompareEdge seemed to compile ok for String, that is:
CompareEdge<String> comparer = new CompareEdge<String>(); worked.
When I check msdn on the String class, it appeared to implement many
interfaces BUT was not generic.

Can someone please educate me why it worked for string?

> Hi there,
>
[quoted text clipped - 30 lines]
> Many thanks,
> J
Peter Duniho - 09 Mar 2008 00:13 GMT
> Jon and Marc, it worked perfectly.  Many thanks.
>
[quoted text clipped - 6 lines]
>
> Can someone please educate me why it worked for string?

The constraint you've provided isn't that the T class itself be generic.  
It's simply that the T class implements the generic interface  
IComparable<T>.

The String class does in fact implement IComparable<String> (well, the  
docs say "IComparable<string>" but that's really the same :) ), and so  
String is a valid parameter to use for T in creating a concrete instance  
of the generic class CompareEdge<T>.

After all, you haven't made your Edge class generic, right?  And yet, as  
long as it implements IComparable<Edge>, it should work in your  
CompareEdge<T> class as well.

All that said, I'm curious as to why you've made CompareEdge<T> a generic  
class at all.  The name implies to me that you'll only ever use it with  
the Edge class.  Conversely, it will be confusing to use the class with a  
class that's not an edge of some sort.  Do you have a variety of classes  
that all represent edges in some way?  If not, why is CompareEdge<T> a  
generic class?

Pete

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.