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. Constructing a Generic Class from several similar classes

Constructing a Generic Class from several similar classes

Scheduled Pinned Locked Moved C#
8 Posts 5 Posters 1 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.
  • W Offline
    W Offline
    Wayne Gaylard
    wrote on last edited by
    #1

    I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-

    public class DecimalClass
    {
    public int x;
    public string y;
    public List<decimal> Details;
    }

    public class StringClass
    {
        public int x;
        public string y;
        public List<string> Details;
    }
    

    Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-

    public class MyClass<T>
    {
    public int x;
    public string y;
    public List<T> Details;
    }

    and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.

    When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

    P L B L 4 Replies Last reply
    0
    • W Wayne Gaylard

      I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-

      public class DecimalClass
      {
      public int x;
      public string y;
      public List<decimal> Details;
      }

      public class StringClass
      {
          public int x;
          public string y;
          public List<string> Details;
      }
      

      Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-

      public class MyClass<T>
      {
      public int x;
      public string y;
      public List<T> Details;
      }

      and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

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

      Hmmmm. Ultimately all classes derive from object, so you could store the data in a List<object>, but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to. Alternatively, you could store it in a Dictionary<Type, List<object>> and use that to avoid having to use the reflection part. Just a thought.

      Forgive your enemies - it messes with their heads

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      W 1 Reply Last reply
      0
      • P Pete OHanlon

        Hmmmm. Ultimately all classes derive from object, so you could store the data in a List<object>, but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to. Alternatively, you could store it in a Dictionary<Type, List<object>> and use that to avoid having to use the reflection part. Just a thought.

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        Hi Pete, Thanks for your input. I think I will have to go with List<object>, but decided to pass the Type in as a parameter to the constructor for the sub class with a private Type variable, as it meant the least changes to existing code, though if I had to start from scratch again, I would have used the dictionary as you suggested. Thanks again for your time.

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        1 Reply Last reply
        0
        • W Wayne Gaylard

          I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-

          public class DecimalClass
          {
          public int x;
          public string y;
          public List<decimal> Details;
          }

          public class StringClass
          {
              public int x;
              public string y;
              public List<string> Details;
          }
          

          Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-

          public class MyClass<T>
          {
          public int x;
          public string y;
          public List<T> Details;
          }

          and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          L Offline
          L Offline
          lukeer
          wrote on last edited by
          #4

          Make the base class

          public class MyBaseClass
          {
          public int x;
          public string y;
          public IEnumerable Details;
          }

          And derive the generic class from it.

          public class MyClass : MyBaseClass
          {
          public MyClass()
          {
          Details = new List();
          }
          }

          That way, you could hold a List baseClassList in your Main class.

          Ciao, luker

          W 1 Reply Last reply
          0
          • L lukeer

            Make the base class

            public class MyBaseClass
            {
            public int x;
            public string y;
            public IEnumerable Details;
            }

            And derive the generic class from it.

            public class MyClass : MyBaseClass
            {
            public MyClass()
            {
            Details = new List();
            }
            }

            That way, you could hold a List baseClassList in your Main class.

            Ciao, luker

            W Offline
            W Offline
            Wayne Gaylard
            wrote on last edited by
            #5

            Unfortunately, holding a List of MyBaseClass I would need to use reflection to cast from MyBaseClass to MyClass in the Main Class and so I would be back to where I started, as I need to know the Type of object I am dealing with. Thanks for your thoughts.

            When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

            L 1 Reply Last reply
            0
            • W Wayne Gaylard

              I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-

              public class DecimalClass
              {
              public int x;
              public string y;
              public List<decimal> Details;
              }

              public class StringClass
              {
                  public int x;
                  public string y;
                  public List<string> Details;
              }
              

              Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-

              public class MyClass<T>
              {
              public int x;
              public string y;
              public List<T> Details;
              }

              and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.

              When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              I think you need to explain more thoroughly what you are actually trying to do. The list of 'lots of stuff' seems confused and that should almost certainly be a List<BaseClass> (or an interface, possibly) where BaseClass defines all the things that you want to do on the different MyClass types. Similar to the answer above, I would factor out the common, non generic parts into a base class (note that this isn't necessarily the same BaseClass referenced in the first paragraph, though it could be; the question is not detailed enough to be able to tell):

              public abstract class BaseClass {
              public int x;
              public string y;
              public virtual IEnumerable BaseDetails { get; set; }
              }

              public class MyClass<T> : BaseClass {
              public IList<T> Details { get; set; }
              public override IEnumerable BaseDetails {
              get { return Details; }
              // The setter will fail if it's the wrong type, probably
              // what you want
              set { Details = (IList<T>)value; }
              }
              }

              BaseClass should also define virtual methods for whatever operations you are going to want to do on things pulled out of the list.

              1 Reply Last reply
              0
              • W Wayne Gaylard

                I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-

                public class DecimalClass
                {
                public int x;
                public string y;
                public List<decimal> Details;
                }

                public class StringClass
                {
                    public int x;
                    public string y;
                    public List<string> Details;
                }
                

                Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-

                public class MyClass<T>
                {
                public int x;
                public string y;
                public List<T> Details;
                }

                and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.

                When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Maybe this helps:

                public class Test {
                public void Run() {
                List<MyBase> all=new List<MyBase>();
                all.Add(new MyGeneric<int>());
                all.Add(new MyGeneric<string>());
                foreach(MyBase b in all) {
                log("Holding a list of "+b.MyType);
                }
                }

                public class MyBase {
                    public Type MyType;
                    public MyBase(Type type) {
                        MyType=type;
                    }
                }
                public class MyGeneric<T> : MyBase {
                    public int x;
                    public string y;
                    public List<T> Details;
                
                    public MyGeneric() : base(typeof(T)) {}
                }
                

                }

                :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                1 Reply Last reply
                0
                • W Wayne Gaylard

                  Unfortunately, holding a List of MyBaseClass I would need to use reflection to cast from MyBaseClass to MyClass in the Main Class and so I would be back to where I started, as I need to know the Type of object I am dealing with. Thanks for your thoughts.

                  When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                  L Offline
                  L Offline
                  lukeer
                  wrote on last edited by
                  #8

                  Don't cast. Use polymorphism instead. Declare the methods you need in MyBaseClass and override them in MyClass. Calling baseClassList[0].SomeMethod() will cause the correct override to execute.

                  Ciao, luker

                  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