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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Passing any object type to a function

Passing any object type to a function

Scheduled Pinned Locked Moved C#
helpquestionregex
8 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.
  • S Offline
    S Offline
    sameercodes
    wrote on last edited by
    #1

    Hi there, I have a function

    List<BaseEntity> ConvertList(List<BaseEntity> listBaseEntity)
    {
    //My code
    }

    Now I am calling the function as follows:

    List<ChildEntity> listChild = new List<ChildEntity>();

    listChild = ConvertList(listChild);

    Here ChildEntity has base class as BaseEntity. On compiling I am getting error as :

    Cannot convert type List<ChildEntity> to List<BaseEntity> via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

    Then I tried defining the function as

    List<object> ConvertList(List<object> listObject)
    {
    //My Code
    }

    Again I am calling the function as follows:

    List<ChildEntity> listChild = new List<ChildEntity>();

    listChild = ConvertList(listChild);

    Then I got the compile time error as

    The best overloaded method match for ConvertList(List<object>)' has some invalid arguments

    How do I resolve this issue? The list that I am passing to the ConvertList function could be of any type. Thanks and Regards - Sameer

    K E 2 Replies Last reply
    0
    • S sameercodes

      Hi there, I have a function

      List<BaseEntity> ConvertList(List<BaseEntity> listBaseEntity)
      {
      //My code
      }

      Now I am calling the function as follows:

      List<ChildEntity> listChild = new List<ChildEntity>();

      listChild = ConvertList(listChild);

      Here ChildEntity has base class as BaseEntity. On compiling I am getting error as :

      Cannot convert type List<ChildEntity> to List<BaseEntity> via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

      Then I tried defining the function as

      List<object> ConvertList(List<object> listObject)
      {
      //My Code
      }

      Again I am calling the function as follows:

      List<ChildEntity> listChild = new List<ChildEntity>();

      listChild = ConvertList(listChild);

      Then I got the compile time error as

      The best overloaded method match for ConvertList(List<object>)' has some invalid arguments

      How do I resolve this issue? The list that I am passing to the ConvertList function could be of any type. Thanks and Regards - Sameer

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      is the object/list type of your function correct? listChild = ConvertList(ref listChild);

      Press F1 for help or google it. Greetings from Germany

      S 1 Reply Last reply
      0
      • K KarstenK

        is the object/list type of your function correct? listChild = ConvertList(ref listChild);

        Press F1 for help or google it. Greetings from Germany

        S Offline
        S Offline
        sameercodes
        wrote on last edited by
        #3

        Hi Karsten, I did the exact way as u'd said but got the following error

        The best overloaded method match for ConvertList(List<BaseEntity>) has some invalid arguments.

        I'll reconstruct the scenario: My function definition is as follows:

        List<BaseEntity> ConvertList(List<BaseEntity>)
        {
        //My code
        }

        And I am passing a child object which is inherited from base entity:

        List<ChildEntity> listChildEntity = new List<ChildEntity>();

        listChildEntity = ConvertList(ref listChildEntity);

        Thanks - Sameer

        T 1 Reply Last reply
        0
        • S sameercodes

          Hi Karsten, I did the exact way as u'd said but got the following error

          The best overloaded method match for ConvertList(List<BaseEntity>) has some invalid arguments.

          I'll reconstruct the scenario: My function definition is as follows:

          List<BaseEntity> ConvertList(List<BaseEntity>)
          {
          //My code
          }

          And I am passing a child object which is inherited from base entity:

          List<ChildEntity> listChildEntity = new List<ChildEntity>();

          listChildEntity = ConvertList(ref listChildEntity);

          Thanks - Sameer

          T Offline
          T Offline
          TheDudeJuan
          wrote on last edited by
          #4

          The parameter in the ConvertList function accepts List but when you call the function you parse a ref of listChildEntity...

          Dollartegn 8D

          S 1 Reply Last reply
          0
          • S sameercodes

            Hi there, I have a function

            List<BaseEntity> ConvertList(List<BaseEntity> listBaseEntity)
            {
            //My code
            }

            Now I am calling the function as follows:

            List<ChildEntity> listChild = new List<ChildEntity>();

            listChild = ConvertList(listChild);

            Here ChildEntity has base class as BaseEntity. On compiling I am getting error as :

            Cannot convert type List<ChildEntity> to List<BaseEntity> via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

            Then I tried defining the function as

            List<object> ConvertList(List<object> listObject)
            {
            //My Code
            }

            Again I am calling the function as follows:

            List<ChildEntity> listChild = new List<ChildEntity>();

            listChild = ConvertList(listChild);

            Then I got the compile time error as

            The best overloaded method match for ConvertList(List<object>)' has some invalid arguments

            How do I resolve this issue? The list that I am passing to the ConvertList function could be of any type. Thanks and Regards - Sameer

            E Offline
            E Offline
            Estys
            wrote on last edited by
            #5

            listChild = ConvertList(listChild.ConvertAll(delegate (ChildEntity a) { return a as BaseEntity}))
            .ConvertAll(delegate (BaseEntity a) { return a as ChildEntity});

            Each item in List<T> must be cast to the appropriate type and back. Do you really need to pass a list? It looks horrible X|

            S 1 Reply Last reply
            0
            • E Estys

              listChild = ConvertList(listChild.ConvertAll(delegate (ChildEntity a) { return a as BaseEntity}))
              .ConvertAll(delegate (BaseEntity a) { return a as ChildEntity});

              Each item in List<T> must be cast to the appropriate type and back. Do you really need to pass a list? It looks horrible X|

              S Offline
              S Offline
              sameercodes
              wrote on last edited by
              #6

              Hi Estys, Thanks for the reply. Yeah, I need to pass a list. Can't help it! :sigh: Actually the ChildEntity is just one example. The BaseEntity is a base class for May child entities, say, ChildEntity1, ChildEntity2, ChildEntity3, etc. Thus, the list could be of any of these types and certain convert actions need to be taken on the same. Your solution seems to be great, but aint it a bit complicated. Is there a simpler one? Thanks and Regards - Sameer

              E 1 Reply Last reply
              0
              • T TheDudeJuan

                The parameter in the ConvertList function accepts List but when you call the function you parse a ref of listChildEntity...

                Dollartegn 8D

                S Offline
                S Offline
                sameercodes
                wrote on last edited by
                #7

                Hi Estys, Thanks for the reply. Actually the ChildEntity is just one example. The BaseEntity is a base class for many child entities, say ChildEntity1, ChildEntity2, ChildEntity3, etc. Thus, the list could be of any of these types and certain convert actions need to be taken on the same. Your solution seems to be great but if you take a look at the replies before, I have tried this and somehow this dint work. I'll reconstruct the scenario again, in detail. My ConvertList function belongs to a static class, say ListClassStatic Thus I am calling the function as:

                listChildList = ListClassStatic.ConvertList(ref listChildList);

                Thanks and Regards - Sameer

                1 Reply Last reply
                0
                • S sameercodes

                  Hi Estys, Thanks for the reply. Yeah, I need to pass a list. Can't help it! :sigh: Actually the ChildEntity is just one example. The BaseEntity is a base class for May child entities, say, ChildEntity1, ChildEntity2, ChildEntity3, etc. Thus, the list could be of any of these types and certain convert actions need to be taken on the same. Your solution seems to be great, but aint it a bit complicated. Is there a simpler one? Thanks and Regards - Sameer

                  E Offline
                  E Offline
                  Estys
                  wrote on last edited by
                  #8

                  This works :

                  static void Conv (BaseEntity baseEntity)
                  {
                  baseEntity.id = "b"; //your code here
                  }

                  Invoke like this :

                  listChild.ForEach(Conv);

                  It doesn't use a list. Your problem with List<T> is that it won't cast to another type.

                  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