Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generics and Constraints

Generics and Constraints

Scheduled Pinned Locked Moved C#
helpquestiontutoriallearning
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J 0
    wrote on last edited by
    #1

    I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie     public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument?  For instance;     public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D

    M L P 3 Replies Last reply
    0
    • J J 0

      I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie     public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument?  For instance;     public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D

      M Offline
      M Offline
      MidwestLimey
      wrote on last edited by
      #2

      Your research is quite (and annoyingly) correct.


      I'm largely language agnostic


      After a while they all bug me :doh:


      J 1 Reply Last reply
      0
      • M MidwestLimey

        Your research is quite (and annoyingly) correct.


        I'm largely language agnostic


        After a while they all bug me :doh:


        J Offline
        J Offline
        J 0
        wrote on last edited by
        #3

        Bah!  Just as I suspected.  Thanks anyway...

        1 Reply Last reply
        0
        • J J 0

          I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie     public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument?  For instance;     public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Your post might imply a factory problem, have you considered a factory solution in your design? Also "Object" is un-typed. It seems that mixing it's use with Generics which are a "Typed" solution might be contradictory depending on the unknown requirements of your project.

          J 1 Reply Last reply
          0
          • J J 0

            I've started learning about generics, and am hoping to implement it in a project at work, to make things a bit easier. I understand you can set a constraint on the generic where, for example, the generic class must have a public empty constructor, ie     public class MyClass<T> where T : new() My question is, is there any way where I can create a constraint where T must have a constructor that accepts an object as an argument?  For instance;     public class MyClass<T> where T : new(Object) I've tried this, and get a compilation error, and from what research I've done in the MSDN documents, it doesn't look like I'll be able to. Thanks for any help :-D

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Unfortunately, as you've found out, the new constraint only states that the constrained object must implement the default parameterless constructor. When you think about it, you can see why it behaves the way that it does. Obviously, having a specific constraint on a constructor would mean that you would be constraining it to a particular type and this type may end up being orthoganal to your generic type. Of course, you could simulate this by doing the following:

            public class MyClass<T, U> where T : new() where U : MyConstrainedClass
            {
              public MyClass(U item)
              {
              }
            }
            

            It's not exactly a neat solution, but it may go someway towards what you are trying to achieve.

            Deja View - the feeling that you've seen this post before.

            1 Reply Last reply
            0
            • L led mike

              Your post might imply a factory problem, have you considered a factory solution in your design? Also "Object" is un-typed. It seems that mixing it's use with Generics which are a "Typed" solution might be contradictory depending on the unknown requirements of your project.

              J Offline
              J Offline
              J 0
              wrote on last edited by
              #6

              Thanks for your reply, but I guess I should've been more clear with my original message.  I don't want to pass a literal Object, I would like to pass an instance of a class that is used for interaction with a database that a coworker has written.  I am not too enthusiastic about using these objects, but I am required to.  I was at least trying to find a way that would make it a little less painstaking.  Thanks for your help though.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups