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. Instantiating a Generic templated class

Instantiating a Generic templated class

Scheduled Pinned Locked Moved C#
comtoolsquestion
8 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.
  • I Offline
    I Offline
    Ista
    wrote on last edited by
    #1

    Hopefully, my last battle with Generics. i have a class like so public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** } Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick

    -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

    A U 2 Replies Last reply
    0
    • I Ista

      Hopefully, my last battle with Generics. i have a class like so public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** } Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick

      -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

      A Offline
      A Offline
      Andy Moore
      wrote on last edited by
      #2

      You would do it like this: public class ForumList : IEnumerable where T : class, new() { public void CreateMe() { T myclass = new T(); } } I hope this helps.

      Deus caritas est

      I 1 Reply Last reply
      0
      • I Ista

        Hopefully, my last battle with Generics. i have a class like so public class ForumList<T> : IEnumerable { public void CreateMe() { **T myclass = new T();** } Is it possible to create an instance of the class or do I need to go through Reflection to do that? Thankis, Nick

        -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

        U Offline
        U Offline
        unclepaul
        wrote on last edited by
        #3

        public class ForumList : IEnumerable where T : new() { public void CreateMe() { T myclass = new T(); }

        1 Reply Last reply
        0
        • A Andy Moore

          You would do it like this: public class ForumList : IEnumerable where T : class, new() { public void CreateMe() { T myclass = new T(); } } I hope this helps.

          Deus caritas est

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          Okay so am I doing this wrong then. Because my ForumList Class should be a collection of Forum objects ( could be other objects too, not just Forum ) List<T> but I need to create new items of type "T" and I also want to extend the List class to provide some custom operations. How should the class declaration look? Is that something like public class ForumList : List<T> where T : class, new() { } -- modified at 12:34 Wednesday 16th August, 2006 -- modified at 12:36 Wednesday 16th August, 2006

          -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

          G 1 Reply Last reply
          0
          • I Ista

            Okay so am I doing this wrong then. Because my ForumList Class should be a collection of Forum objects ( could be other objects too, not just Forum ) List<T> but I need to create new items of type "T" and I also want to extend the List class to provide some custom operations. How should the class declaration look? Is that something like public class ForumList : List<T> where T : class, new() { } -- modified at 12:34 Wednesday 16th August, 2006 -- modified at 12:36 Wednesday 16th August, 2006

            -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            These should (at least be close to) be working: If you want to inherit List<>:

            public class ForumList<T> : List<T> where T : class, new() {
            public ForumList() {}
            public T AddNewItem() {
            T item = new T();
            this.Add(item);
            return item;
            }
            }

            If you want to keep the list private in the class:

            public class FourmList<T> : IEnumerable<T> where T : class, new() {
            private List<T> list;
            public ForumList() {
            this.list = new List<T>();
            }
            public T AddNewItem() {
            T item = new T();
            this.list.Add(item);
            return item;
            }
            public IEnumerator<T> GetEnumerator() {
            return (IEnumerator<T>)this.list.GetEnumerator();
            }
            }

            --- b { font-weight: normal; }

            I 1 Reply Last reply
            0
            • G Guffa

              These should (at least be close to) be working: If you want to inherit List<>:

              public class ForumList<T> : List<T> where T : class, new() {
              public ForumList() {}
              public T AddNewItem() {
              T item = new T();
              this.Add(item);
              return item;
              }
              }

              If you want to keep the list private in the class:

              public class FourmList<T> : IEnumerable<T> where T : class, new() {
              private List<T> list;
              public ForumList() {
              this.list = new List<T>();
              }
              public T AddNewItem() {
              T item = new T();
              this.list.Add(item);
              return item;
              }
              public IEnumerator<T> GetEnumerator() {
              return (IEnumerator<T>)this.list.GetEnumerator();
              }
              }

              --- b { font-weight: normal; }

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?

              -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

              G U 2 Replies Last reply
              0
              • I Ista

                Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?

                -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                The "new()" constraint only specifies that the class have to have a parameterless constructor. You can't specify any constraint for overloaded constructors.

                --- b { font-weight: normal; }

                1 Reply Last reply
                0
                • I Ista

                  Thanks Guffa. So one last question. does the "new()" take care of all constructors(overloaded parameters) or do I list out each possible one like new(int) I.e.. new(int id) ?

                  -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

                  U Offline
                  U Offline
                  unclepaul
                  wrote on last edited by
                  #8

                  Nope, your T must have a parameterless constructor

                  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