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. Setting different properties based on T in method that returns List<T>

Setting different properties based on T in method that returns List<T>

Scheduled Pinned Locked Moved C#
questionhelptutorial
12 Posts 5 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.
  • P Offline
    P Offline
    Paul E Davies
    wrote on last edited by
    #1

    Hi All, I have a method that returns List, T can be a number of types but based on the type I want set different properties. I can detect the type passed into the method and can switch on the name but I'm a bit lost on how to convert my List to list so the return type is correct. the error is Cannot implicitly convert type List to List which is understandable but how do I return List as List where T is type?

    P 1 Reply Last reply
    0
    • P Paul E Davies

      Hi All, I have a method that returns List, T can be a number of types but based on the type I want set different properties. I can detect the type passed into the method and can switch on the name but I'm a bit lost on how to convert my List to list so the return type is correct. the error is Cannot implicitly convert type List to List which is understandable but how do I return List as List where T is type?

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

      Well, type (in your example), must be derived from type T, so the easiest way to tell it to do this would be to put a constraint on T in your declaration, e.g.

      public List<T> MyMethod<T>(Type type) where T : MyBaseType
      {
      }

      Forgive your enemies - it messes with their heads

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

      P 1 Reply Last reply
      0
      • P Pete OHanlon

        Well, type (in your example), must be derived from type T, so the easiest way to tell it to do this would be to put a constraint on T in your declaration, e.g.

        public List<T> MyMethod<T>(Type type) where T : MyBaseType
        {
        }

        Forgive your enemies - it messes with their heads

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

        P Offline
        P Offline
        Paul E Davies
        wrote on last edited by
        #3

        Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help

        public List getall()where T : employee
        {
        type t = typeof(T)
        switch (t.name)
        {
        case "manager":
        List all = this.GetManagers()
        break;
        default
        List all = this.getSales()

        }
        
        return all 
        

        }

        This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.

        P D P 3 Replies Last reply
        0
        • P Paul E Davies

          Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help

          public List getall()where T : employee
          {
          type t = typeof(T)
          switch (t.name)
          {
          case "manager":
          List all = this.GetManagers()
          break;
          default
          List all = this.getSales()

          }
          
          return all 
          

          }

          This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.

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

          You're starting to get there, but you need to cast to the appropriate list type as in:

             public List<T> GetAll<T>() where T : Employee
              {
                  List<T> all = new List<T>();
                  if (typeof(T) == typeof(Manager))
                      all = GetManagers() as List<T>;
                  else
                      all = GetSales() as List<T>;
                  return all;
              }
          

          Forgive your enemies - it messes with their heads

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

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            You're starting to get there, but you need to cast to the appropriate list type as in:

               public List<T> GetAll<T>() where T : Employee
                {
                    List<T> all = new List<T>();
                    if (typeof(T) == typeof(Manager))
                        all = GetManagers() as List<T>;
                    else
                        all = GetSales() as List<T>;
                    return all;
                }
            

            Forgive your enemies - it messes with their heads

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

            P Offline
            P Offline
            Paul E Davies
            wrote on last edited by
            #5

            Ah yes, got it! Thank you very much I didn't think of casting the whole list. Much appreciated. Paul

            P 1 Reply Last reply
            0
            • P Paul E Davies

              Ah yes, got it! Thank you very much I didn't think of casting the whole list. Much appreciated. Paul

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

              No problems. Glad to be of service.

              Forgive your enemies - it messes with their heads

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

              1 Reply Last reply
              0
              • P Paul E Davies

                Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help

                public List getall()where T : employee
                {
                type t = typeof(T)
                switch (t.name)
                {
                case "manager":
                List all = this.GetManagers()
                break;
                default
                List all = this.getSales()

                }
                
                return all 
                

                }

                This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.

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

                I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,

                var managers = getall();

                you might as well call a method with a different name, like this:

                var managers = getAllManagers();

                In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?

                B P 2 Replies Last reply
                0
                • D dasblinkenlight

                  I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,

                  var managers = getall();

                  you might as well call a method with a different name, like this:

                  var managers = getAllManagers();

                  In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?

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

                  I agree. It seems like the problem here is showing up the underlying inconsistency in what the OP is trying to do. I don't see the purpose of this method.

                  1 Reply Last reply
                  0
                  • P Paul E Davies

                    Thanks Pete, I've tried putting constraints on the method but I'm still getting the error, I think I'm doing something fundamentally wrong. perhaps an example would help

                    public List getall()where T : employee
                    {
                    type t = typeof(T)
                    switch (t.name)
                    {
                    case "manager":
                    List all = this.GetManagers()
                    break;
                    default
                    List all = this.getSales()

                    }
                    
                    return all 
                    

                    }

                    This is something along the lines of what I'm trying to do (I haven't tried compiling it but it's the types I have the issue with, not syntax)- Imagine both Sales and managers are of type employee but managers are paid monthly and do not get commission but sales are paid weekly and do get commission, so whilst they have much in common I want to treat them differently. Hope this makes more sense.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Paul E Davies wrote:

                    List<manager> all = this.GetManagers()

                    List<T> all = this.GetManagers()

                    1 Reply Last reply
                    0
                    • D dasblinkenlight

                      I'm curious what are you trying to achieve with a method like this. Since you are forced to specify an exact type at the point of call anyway, for example,

                      var managers = getall();

                      you might as well call a method with a different name, like this:

                      var managers = getAllManagers();

                      In other words, your getall method hides certain details, yet the caller is forced to reveal these details back! Could you please explain your intent?

                      P Offline
                      P Offline
                      Paul E Davies
                      wrote on last edited by
                      #10

                      I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.

                      D B 2 Replies Last reply
                      0
                      • P Paul E Davies

                        I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.

                        D Offline
                        D Offline
                        dasblinkenlight
                        wrote on last edited by
                        #11

                        Are you certain that you need a generic for this scenario? It sounds like a simpler List<employee> would do just fine, no?

                        1 Reply Last reply
                        0
                        • P Paul E Davies

                          I'm sure there are a number of ways to do it, this is the scenario I have been presented with. I have some big text files and depending on their origin will contain slightly differing sets of data. I have regex patterns that will return a collection of matches - each match describes an object of type T, Once I have the matches I know what type T is and can process the list on this basis. 90% of the processing is the same but differs slightly based on T. Hope that makes some sense.

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

                          I would use a List<Employee>, but instantiate the appropriate type of Employee and have some virtual methods that you can override to get the custom behaviour for each one.

                          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