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. using a List<> object in an abstract class

using a List<> object in an abstract class

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 2 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.
  • V Vodstok

    I have an abstract class I am writing that i want to inherit from multiple other classes. i am running into an issue, however. i have the following : protected abstract List<> GetList(); I am trying, if possible to make it that the classes that inherit this one can set the type for the list. so in class 1 it would be public override List GetList() class 2 public override List<int> GetList() clss 3 public override List<float> GetList() Is this possible? what do i use in tthe inherited list<> object?

    ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

    G Offline
    G Offline
    Giorgi Dalakishvili
    wrote on last edited by
    #2

    Make your class generic

    Giorgi Dalakishvili #region signature my articles #endregion

    V 1 Reply Last reply
    0
    • G Giorgi Dalakishvili

      Make your class generic

      Giorgi Dalakishvili #region signature my articles #endregion

      V Offline
      V Offline
      Vodstok
      wrote on last edited by
      #3

      beautiful. Just went to MSDN and gave myself a quick lesson in genericmethods, and now I'm cooking with gas. Thank you!

      ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

      G 1 Reply Last reply
      0
      • V Vodstok

        beautiful. Just went to MSDN and gave myself a quick lesson in genericmethods, and now I'm cooking with gas. Thank you!

        ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #4

        You are welcome :)

        Giorgi Dalakishvili #region signature my articles #endregion

        V 1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          You are welcome :)

          Giorgi Dalakishvili #region signature my articles #endregion

          V Offline
          V Offline
          Vodstok
          wrote on last edited by
          #5

          I have another one for you if you are interested. Same abstract class, i have a Get() method. Similar problem, only rather than returning a List<> object, it is for individual objects, so class one would be public override int Get(), class 2 public override string Get(), etc.

          ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

          G 1 Reply Last reply
          0
          • V Vodstok

            I have another one for you if you are interested. Same abstract class, i have a Get() method. Similar problem, only rather than returning a List<> object, it is for individual objects, so class one would be public override int Get(), class 2 public override string Get(), etc.

            ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #6

            You can use T as a return type of the Get() method

            Giorgi Dalakishvili #region signature my articles #endregion

            V 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              You can use T as a return type of the Get() method

              Giorgi Dalakishvili #region signature my articles #endregion

              V Offline
              V Offline
              Vodstok
              wrote on last edited by
              #7

              Thank you again. :) I have actually worked with these before, but that was a project for another company, 6 months ago, so I was having a hard time remembering the details :) You are a life saver (and a time saver)

              ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

              G 1 Reply Last reply
              0
              • V Vodstok

                Thank you again. :) I have actually worked with these before, but that was a project for another company, 6 months ago, so I was having a hard time remembering the details :) You are a life saver (and a time saver)

                ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #8

                Thanks man :)

                Giorgi Dalakishvili #region signature my articles #endregion

                V 1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  Thanks man :)

                  Giorgi Dalakishvili #region signature my articles #endregion

                  V Offline
                  V Offline
                  Vodstok
                  wrote on last edited by
                  #9

                  I have moved on to another class inheriting this class, and have found a new problem. here is my code: public abstract List<T> GetList<T>() where T : Class1,new(); here is my issue. now i am trying to use the same abstract class to make another class, so this one would need to look like: public abstract List&lt;T&gt; GetList&lt;T&gt;() where T : Class2,new(); however, i cant have them both in the same abstract class, and i cant seem to figure out how to do essentially this (i know this here doesnt work): public abstract List&amp;lt;T&amp;gt; GetList&amp;lt;T&amp;gt;() where T : (CLass1,Class2),new(); Any ideas?

                  ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                  G 1 Reply Last reply
                  0
                  • V Vodstok

                    I have moved on to another class inheriting this class, and have found a new problem. here is my code: public abstract List<T> GetList<T>() where T : Class1,new(); here is my issue. now i am trying to use the same abstract class to make another class, so this one would need to look like: public abstract List&lt;T&gt; GetList&lt;T&gt;() where T : Class2,new(); however, i cant have them both in the same abstract class, and i cant seem to figure out how to do essentially this (i know this here doesnt work): public abstract List&amp;lt;T&amp;gt; GetList&amp;lt;T&amp;gt;() where T : (CLass1,Class2),new(); Any ideas?

                    ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #10

                    As you know C# does not support multiple inheritance of implementation so at most one base class can be used in a constraint. Just read the documentation: An Introduction to C# Generics[^]

                    Giorgi Dalakishvili #region signature my articles #endregion

                    V 1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      As you know C# does not support multiple inheritance of implementation so at most one base class can be used in a constraint. Just read the documentation: An Introduction to C# Generics[^]

                      Giorgi Dalakishvili #region signature my articles #endregion

                      V Offline
                      V Offline
                      Vodstok
                      wrote on last edited by
                      #11

                      Hmmm.... I will need to figure out another way. maybe i should not be bothering to include it in the bastract class, for now. this is my project and i am the architect and sole programmer,so it should not be thta big a deal. Thank you again for all of your help. :)

                      ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                      G 1 Reply Last reply
                      0
                      • V Vodstok

                        Hmmm.... I will need to figure out another way. maybe i should not be bothering to include it in the bastract class, for now. this is my project and i am the architect and sole programmer,so it should not be thta big a deal. Thank you again for all of your help. :)

                        ______________________ Mr Griffin, eleventy billion is not a number...:wtf:

                        G Offline
                        G Offline
                        Giorgi Dalakishvili
                        wrote on last edited by
                        #12

                        If you want your T parameter to inherit from Class1 and Class2 it means that either Class1 must inherit from Class2 or Class2 from Class1. In either cases you will need to specify just one class as a base class in constraint list as it will automatically mean that T inherits from the second class too.

                        Giorgi Dalakishvili #region signature my articles #endregion

                        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