Generics, constrains and inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Generics, constrains and inheritance

    Hi All!

    Here's the problem I'm having. I want to create a generic class that
    inherits from CollectionBase and implements IBindingList. As part of
    IBindingList I have to implement AddNew(). Since this is a generic class the
    new throws an error "Cannot create an instance of the variable type 'T'
    because it does not have the new constraint".

    How do I add a constraint to my class and be able to keep the CollectionBase
    and IBindingList?

    When I add the where <T>: it means that I want the class T to implement
    CollectionBase and IBindingList.

    Is it possible to do what I want?

    Thanks,
    Joe


  • Joe

    #2
    Re: Generics, constrains and inheritance

    I believe I have my answer. I need to declare my class like this:

    class MyClass<T: CollectionBase, IBindingList where T: new ()
    {
    ....
    }

    "Joe" <jbassking@noem ail.noemailwrot e in message
    news:e0tqttrMJH A.1156@TK2MSFTN GP05.phx.gbl...
    Hi All!
    >
    Here's the problem I'm having. I want to create a generic class that
    inherits from CollectionBase and implements IBindingList. As part of
    IBindingList I have to implement AddNew(). Since this is a generic class
    the new throws an error "Cannot create an instance of the variable type
    'T' because it does not have the new constraint".
    >
    How do I add a constraint to my class and be able to keep the
    CollectionBase and IBindingList?
    >
    When I add the where <T>: it means that I want the class T to implement
    CollectionBase and IBindingList.
    >
    Is it possible to do what I want?
    >
    Thanks,
    Joe
    >
    >

    Comment

    • puzzlecracker

      #3
      Re: Generics, constrains and inheritance

      On Oct 20, 10:10 am, "Joe" <jbassk...@noem ail.noemailwrot e:
      I believe I have my answer. I need to declare my class like this:
      >
      class MyClass<T: CollectionBase, IBindingList where T: new ()
      {
          ....
      >
      }
      I don't think compiler knows what t where T: new () means...

      Comment

      • Peter Duniho

        #4
        Re: Generics, constrains and inheritance

        On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker <ironsel2000@gm ail.com>
        wrote:
        On Oct 20, 10:10 am, "Joe" <jbassk...@noem ail.noemailwrot e:
        >I believe I have my answer. I need to declare my class like this:
        >>
        >class MyClass<T: CollectionBase, IBindingList where T: new ()
        >{
        >    ....
        >>
        >}
        I don't think compiler knows what t where T: new () means...
        Why don't you think that?


        Comment

        • puzzlecracker

          #5
          Re: Generics, constrains and inheritance

          On Oct 20, 4:18 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
          wrote:
          On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker <ironsel2...@gm ail.com> 
          wrote:
          >
          On Oct 20, 10:10 am, "Joe" <jbassk...@noem ail.noemailwrot e:
          I believe I have my answer. I need to declare my class like this:
          >
          class MyClass<T: CollectionBase, IBindingList where T: new ()
          {
              ....
          >
          }
          I don't think compiler knows what t where T: new () means...
          >
          Why don't you think that?
          >
          http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx
          I should have rtfm'ed. I didn't know that you can constrain types
          based on different constructor types... learn something new every
          day...

          Comment

          • Ben Voigt [C++ MVP]

            #6
            Re: Generics, constrains and inheritance

            puzzlecracker wrote:
            On Oct 20, 4:18 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
            wrote:
            >On Mon, 20 Oct 2008 12:57:26 -0700, puzzlecracker
            ><ironsel2...@g mail.comwrote:
            >>
            >>On Oct 20, 10:10 am, "Joe" <jbassk...@noem ail.noemailwrot e:
            >>>I believe I have my answer. I need to declare my class like this:
            >>
            >>>class MyClass<T: CollectionBase, IBindingList where T: new ()
            >>>{
            >>>....
            >>
            >>>}
            >>I don't think compiler knows what t where T: new () means...
            >>
            >Why don't you think that?
            >>
            >http://msdn.microsoft.com/en-us/library/sd2w2ew5.aspx
            >
            I should have rtfm'ed. I didn't know that you can constrain types
            based on different constructor types... learn something new every
            day...
            You can't. At least not the part about different constructor types. There
            is a constraint for "public parameterless constructor" and that's it, sadly.


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Generics, constrains and inheritance

              On Oct 20, 10:22 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
              I should have rtfm'ed. I didn't know that you can constrain types
              based on different constructor types... learn something new every
              day...
              >
              You can't.  At least not the part about different constructor types.  There
              is a constraint for "public parameterless constructor" and that's it, sadly.
              For a suggestion of how an alternative might work, see


              Jon

              Comment

              Working...