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 problem

Generics problem

Scheduled Pinned Locked Moved C#
helpquestion
7 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.
  • K Offline
    K Offline
    kapax5
    wrote on last edited by
    #1

    There are following classes:

    public class X
    {
    Int32[] a;
    public List<Int32> xxx()
    { ... }
    }

    public class Y
    {
    List<Int32> a;
    public List<Int32> xxx()
    { ... }
    }

    What I am trying to do is to find a generic way to make the class of similar descent:

    public class GenericClass<T> where T : IEnumerable
    {
    T a;
    public List</*Type of the elements in the IEnumerable T*/> xxx()
    { .... }
    }

    No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)

    E L D 3 Replies Last reply
    0
    • K kapax5

      There are following classes:

      public class X
      {
      Int32[] a;
      public List<Int32> xxx()
      { ... }
      }

      public class Y
      {
      List<Int32> a;
      public List<Int32> xxx()
      { ... }
      }

      What I am trying to do is to find a generic way to make the class of similar descent:

      public class GenericClass<T> where T : IEnumerable
      {
      T a;
      public List</*Type of the elements in the IEnumerable T*/> xxx()
      { .... }
      }

      No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      public class Foo<T> : IEnumerable<T> {
      private List<T> someList;
      IEnumerator<T> IEnumerable<T>.GetEnumerator();
      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator();
      }

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      1 Reply Last reply
      0
      • K kapax5

        There are following classes:

        public class X
        {
        Int32[] a;
        public List<Int32> xxx()
        { ... }
        }

        public class Y
        {
        List<Int32> a;
        public List<Int32> xxx()
        { ... }
        }

        What I am trying to do is to find a generic way to make the class of similar descent:

        public class GenericClass<T> where T : IEnumerable
        {
        T a;
        public List</*Type of the elements in the IEnumerable T*/> xxx()
        { .... }
        }

        No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)

        L Offline
        L Offline
        Lukasz Nowakowski
        wrote on last edited by
        #3

        I don't know if I understand your problem, but if I'm correct, you should do public class GenericClass<T> { T a; public List<T> xxx() { .... } } If it's not what you want, write about your problem in more details.

        Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

        K 1 Reply Last reply
        0
        • L Lukasz Nowakowski

          I don't know if I understand your problem, but if I'm correct, you should do public class GenericClass<T> { T a; public List<T> xxx() { .... } } If it's not what you want, write about your problem in more details.

          Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

          K Offline
          K Offline
          kapax5
          wrote on last edited by
          #4

          You see, if I instantiate this class like this:

          new GenericClass<Int32[]>();

          , then what xxx() returns is List<Int32[]>, although it should return List<Int32>. Now, if I instantiate the class this way:

          new GenericClass<List<Int32>>();

          , then xxx() will return List<List<Int32>>, but it should also return List<Int32>. That is why I think that IEnumerable is the way to go for T. I am starting to think that my idea is conceptually wrong, since I cannot think of anything that would work.

          1 Reply Last reply
          0
          • K kapax5

            There are following classes:

            public class X
            {
            Int32[] a;
            public List<Int32> xxx()
            { ... }
            }

            public class Y
            {
            List<Int32> a;
            public List<Int32> xxx()
            { ... }
            }

            What I am trying to do is to find a generic way to make the class of similar descent:

            public class GenericClass<T> where T : IEnumerable
            {
            T a;
            public List</*Type of the elements in the IEnumerable T*/> xxx()
            { .... }
            }

            No ideas come to my head. Maybe some will come to yours'? I understand that the problem is that IEnumerable is not generic but maybe there will be any clever solutions :)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Your problem is you need T to be the type of element, not a collection of so your IEnumerable idea isn't going to work. I think you need something like this:

            public class GenericClass<T>
            {
            private List<T> a = null;

            public GenericClass(IEnumerable<T> items)
            {
                if (items != null)
                    a = new List<T>(items);
            }
            
            public List<T> Xxx()
            {
                return a;
            }
            

            }

            Dave

            If this helped, please vote & accept answer!

            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            K 1 Reply Last reply
            0
            • D DaveyM69

              Your problem is you need T to be the type of element, not a collection of so your IEnumerable idea isn't going to work. I think you need something like this:

              public class GenericClass<T>
              {
              private List<T> a = null;

              public GenericClass(IEnumerable<T> items)
              {
                  if (items != null)
                      a = new List<T>(items);
              }
              
              public List<T> Xxx()
              {
                  return a;
              }
              

              }

              Dave

              If this helped, please vote & accept answer!

              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              K Offline
              K Offline
              kapax5
              wrote on last edited by
              #6

              Yes, that's what I needed! Simple and elegant. Thank you!

              D 1 Reply Last reply
              0
              • K kapax5

                Yes, that's what I needed! Simple and elegant. Thank you!

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                You're welcome :)

                Dave

                If this helped, please vote & accept answer!

                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                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