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 return type cannot cast.

Generics return type cannot cast.

Scheduled Pinned Locked Moved C#
databasehelpquestionlounge
3 Posts 3 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.
  • N Offline
    N Offline
    NT986
    wrote on last edited by
    #1

    I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:

    public class cList<T>
    {
    public cList(cIDList UseOwner)
    {
    Owner = UseOwner;
    ListItems = new ArrayList();
    }

    public T this\[int Index\]
    {
        get
    {
        if ((Index >= 0) && (Index < ListItems.Count))
        {    return (T) Owner.GetObject((int)ListItems\[Index\]);    //\*\*\*problem cannot implicitely convert to type T
        }
    }
    }
    private cIDList Owner;
    private ArrayList ListItems;	//would contain 2,7,9
    

    }

    to implement this
    cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
    (assume items added)
    cTypeA UseA = AItem[2];

    Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.

    T P 2 Replies Last reply
    0
    • N NT986

      I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:

      public class cList<T>
      {
      public cList(cIDList UseOwner)
      {
      Owner = UseOwner;
      ListItems = new ArrayList();
      }

      public T this\[int Index\]
      {
          get
      {
          if ((Index >= 0) && (Index < ListItems.Count))
          {    return (T) Owner.GetObject((int)ListItems\[Index\]);    //\*\*\*problem cannot implicitely convert to type T
          }
      }
      }
      private cIDList Owner;
      private ArrayList ListItems;	//would contain 2,7,9
      

      }

      to implement this
      cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
      (assume items added)
      cTypeA UseA = AItem[2];

      Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.

      T Offline
      T Offline
      Thomas Weller 0
      wrote on last edited by
      #2

      You can use a generic List instead of an untyped ArrayList, then copy the required references in the c'tor. (This assumes you are using .NET 3.5 - and of course your cIDList must be an IEnumerable, i.e. derived from any of the usual .NET collection classes). Here's what it will look like:

      public class cList<T>
      {
      private readonly List<T> ListItems = new List<T>();

      public cList(cIDList UseOwner)
      {
          if (UseOwner != null)
          { 
              ListItems.AddRange(UseOwner.OfType<T>());
          }
      }
      
      public T this\[int Index\]
      {       
          get
          {
              if ((Index >= 0) && (Index < ListItems.Count))
              {
                  return ListItems\[Index\];
              }
            
              return null; // or default(T) or even better: throw an ArgumentOutOfRangeException...
         }
      }
      

      }

      Regards Thomas

      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
      Programmer - an organism that turns coffee into software.

      modified on Sunday, May 3, 2009 11:35 AM

      1 Reply Last reply
      0
      • N NT986

        I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:

        public class cList<T>
        {
        public cList(cIDList UseOwner)
        {
        Owner = UseOwner;
        ListItems = new ArrayList();
        }

        public T this\[int Index\]
        {
            get
        {
            if ((Index >= 0) && (Index < ListItems.Count))
            {    return (T) Owner.GetObject((int)ListItems\[Index\]);    //\*\*\*problem cannot implicitely convert to type T
            }
        }
        }
        private cIDList Owner;
        private ArrayList ListItems;	//would contain 2,7,9
        

        }

        to implement this
        cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
        (assume items added)
        cTypeA UseA = AItem[2];

        Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.

        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #3

        You probably need to redesign whatever it is you're trying to do. Otherwise, try cTypeA UseA = AItem[2] as cTypeA ;

        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