[RESOLVED] Generics question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Generics question

  1. #1
    Join Date
    Mar 2010
    Posts
    7

    [RESOLVED] Generics question

    I have a question about generics. I was wondering if there was a way to rewrite this:

    class ByFirstNameComparer : IComparer<Person>
    {
    public int Compare(<Person> p1, <Person> p2)
    {...}
    }

    To something like this:

    class ByFirstNameComparer : IComparer<T> where T is Person
    {
    public int Compare(<T> p1, <T> p2)
    {...}
    }

    .net 4.0? VS2010 Ultimate V10.0.30128.1 RC1Rel

    Thanks,
    Matthew

  2. #2
    Join Date
    Mar 2010
    Posts
    7

    Re: Generics question

    err (Person p1, Person p2)

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,537

    Re: Generics question

    Quote Originally Posted by MattInSD73 View Post
    err (Person p1, Person p2)
    Code:
    public class ByFirstNameComparer<T> : IComparer<T> where T : Person
    {
         public int Compare(T p1, T p2){
             return //..... your code here
         }
    }
    
    public class Person { }
    This should work
    Last edited by JonnyPoet; March 27th, 2010 at 05:07 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Mar 2010
    Posts
    7

    Re: Generics question

    Thanks! I thought there was a was to make it more "loosely coupled" than I had it written originally(if that's the correct term).

    ~M

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center