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 question

Generics question

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

    Hi, I got a problem that I would like to return a generic object in a non-generic object, what can I do? I searched a lot of page but it's talking about how to use or implement a generic class, but if I want to implement a normal class and return a generic object according parameter, what can I do? e.g. class DaoFactory { Dao<T, IdT> GetDao(Type t) { //something like this. //how to implement? } } Thanks Jr. P calendarw

    A 1 Reply Last reply
    0
    • C calendarw

      Hi, I got a problem that I would like to return a generic object in a non-generic object, what can I do? I searched a lot of page but it's talking about how to use or implement a generic class, but if I want to implement a normal class and return a generic object according parameter, what can I do? e.g. class DaoFactory { Dao<T, IdT> GetDao(Type t) { //something like this. //how to implement? } } Thanks Jr. P calendarw

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      How about this one? /// /// Convert an ArrayList from a derived type to the base type /// /// Derived type /// Base type /// The ArrayList /// An ArrayList containing elements converted to the base type public static List Transform(List list) where S : D { List lst = new List(); foreach (S element in list) lst.Add(element); return lst; } I will use Google before asking dumb questions

      C 1 Reply Last reply
      0
      • A Andrei Ungureanu

        How about this one? /// /// Convert an ArrayList from a derived type to the base type /// /// Derived type /// Base type /// The ArrayList /// An ArrayList containing elements converted to the base type public static List Transform(List list) where S : D { List lst = new List(); foreach (S element in list) lst.Add(element); return lst; } I will use Google before asking dumb questions

        C Offline
        C Offline
        calendarw
        wrote on last edited by
        #3

        Thank you. I changed to this: public static List<D> Transform<D>(System.Collections.IList list) { List<D> lst = new List<D>(); foreach (D element in list) lst.Add(element); return lst; } Actually, I tried to search using google but I don't know what keyword should be used for this case.

        A 1 Reply Last reply
        0
        • C calendarw

          Thank you. I changed to this: public static List<D> Transform<D>(System.Collections.IList list) { List<D> lst = new List<D>(); foreach (D element in list) lst.Add(element); return lst; } Actually, I tried to search using google but I don't know what keyword should be used for this case.

          A Offline
          A Offline
          Andrei Ungureanu
          wrote on last edited by
          #4

          How about [this] Hope it helps.

          I will use Google before asking dumb questions

          C 1 Reply Last reply
          0
          • A Andrei Ungureanu

            How about [this] Hope it helps.

            I will use Google before asking dumb questions

            C Offline
            C Offline
            calendarw
            wrote on last edited by
            #5

            Thank you for your information. And I would like to implement something like this: public interface IReadOnlyDao { object GetById(object id); IList GetAll(); } public interface IReadOnlyDao<T, IdT> : IReadOnlyDao { T GetById(IdT id); IList<T, IdT> GetAll(); } But when I implementing the GetAll(), I got a problem that two method are same name and same parameter, is it possible to implement? or I am required to use convert the list using previous discuss method. Thank you very much.

            A 1 Reply Last reply
            0
            • C calendarw

              Thank you for your information. And I would like to implement something like this: public interface IReadOnlyDao { object GetById(object id); IList GetAll(); } public interface IReadOnlyDao<T, IdT> : IReadOnlyDao { T GetById(IdT id); IList<T, IdT> GetAll(); } But when I implementing the GetAll(), I got a problem that two method are same name and same parameter, is it possible to implement? or I am required to use convert the list using previous discuss method. Thank you very much.

              A Offline
              A Offline
              Andrei Ungureanu
              wrote on last edited by
              #6

              I think it will be better to use the converter. My opinion

              I will use Google before asking dumb questions

              C 1 Reply Last reply
              0
              • A Andrei Ungureanu

                I think it will be better to use the converter. My opinion

                I will use Google before asking dumb questions

                C Offline
                C Offline
                calendarw
                wrote on last edited by
                #7

                Thank you very much~~ :):):):)

                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