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

Tip: Looking for answers? Try searching our database.

Generics question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dilip - 31 Jan 2008 21:31 GMT
I boxed myself to a corner with this design.  I wanted classes
implementing X to necessarily implement IEquatable.  I thought I'd go
the C++ CRTP route and came up with this.

interface X<T> : IEquatable<T>
{
}

class XImpl : X<XImpl>
{
}

class XImpl_2 : X<XImpl_2>
{
}

then I had a class to manage a collection of objects implementing X

class X_Collection<T> where T : X<T>
{
  private LinkedList<T> XCollection = new LinkedList<T>();
}

Now I am stuck here.

How do I declare an instance of X_Collection when I have no idea what
T would be at that point?  The only thing I can do is:

class someClass
{
  private X_Collection<XImpl> col = new X_Collection<XImpl>();
}

I don't want that since the whole idea is to use the X_Collection
class to keep track of *any* object that implements X<T>.

I seem to be in some kind of recursive nightmare.

Can anyone help?
Ben Voigt [C++ MVP] - 31 Jan 2008 21:44 GMT
>I boxed myself to a corner with this design.  I wanted classes
> implementing X to necessarily implement IEquatable.  I thought I'd go
[quoted text clipped - 35 lines]
>
> Can anyone help?

According to the LSP, you can't provide a strong guarantee that you only
compare the object to the same implementation type, the comparand could be
any X, so use

interface X : IEquatable<X>
{
}

and everything should turn out ok.
Dilip - 31 Jan 2008 21:50 GMT
> >I boxed myself to a corner with this design.  I wanted classes
> > implementing X to necessarily implement IEquatable.  I thought I'd go
[quoted text clipped - 46 lines]
>
> and everything should turn out ok.

Cheers Ben!  My mind has successfully disentangled itself :-)
Ben Voigt [C++ MVP] - 31 Jan 2008 23:36 GMT
>> >I boxed myself to a corner with this design.  I wanted classes
>> > implementing X to necessarily implement IEquatable.  I thought I'd go
[quoted text clipped - 49 lines]
>
> Cheers Ben!  My mind has successfully disentangled itself :-)

I'm glad for you.  Practically nothing that CRTP is good for in C++ works in
.NET, primarily because generics don't late-bind like templates.

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.