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. How do I cast a generic list to a generic interface

How do I cast a generic list to a generic interface

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 3 Posters 2 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.
  • M Offline
    M Offline
    MBursill
    wrote on last edited by
    #1

    I have the following interface

    public interface IImage
    {
    string FileName { get; set;}
    string Caption { get; set;}
    }

    I have a class called Image that implements the interface. I create a BindingList of that Image object:

    BindingList<Image> images = new BindingList<Image>();

    I later want to store images in a list defined as the interface.

    BindingList<IImage> = images; // casting error

    Why am I getting a casting error? If I understand the rules of polymorphisms correctly, so long as my Image class implements IImage, I can store any Image as an IImage without needing to explicitly cast. What to do? :confused: -Mike.

    M L 2 Replies Last reply
    0
    • M MBursill

      I have the following interface

      public interface IImage
      {
      string FileName { get; set;}
      string Caption { get; set;}
      }

      I have a class called Image that implements the interface. I create a BindingList of that Image object:

      BindingList<Image> images = new BindingList<Image>();

      I later want to store images in a list defined as the interface.

      BindingList<IImage> = images; // casting error

      Why am I getting a casting error? If I understand the rules of polymorphisms correctly, so long as my Image class implements IImage, I can store any Image as an IImage without needing to explicitly cast. What to do? :confused: -Mike.

      M Offline
      M Offline
      Manoj Kumar Rai
      wrote on last edited by
      #2

      MBursill wrote:

      BindingList = images; // casting error Why am I getting a casting error? If I understand the rules of polymorphisms correctly, so long as my Image class implements IImage, I can store any Image as an IImage without needing to explicitly cast.

      See, as per polymorphisms you asign a object of the drived class to a base class. But in the above case the "BindingList" is not a base class to the "BindingList", hence you are not able to assign them. Its actually the items they contains have relation of base class - drive class.

      Manoj Never Gives up

      1 Reply Last reply
      0
      • M MBursill

        I have the following interface

        public interface IImage
        {
        string FileName { get; set;}
        string Caption { get; set;}
        }

        I have a class called Image that implements the interface. I create a BindingList of that Image object:

        BindingList<Image> images = new BindingList<Image>();

        I later want to store images in a list defined as the interface.

        BindingList<IImage> = images; // casting error

        Why am I getting a casting error? If I understand the rules of polymorphisms correctly, so long as my Image class implements IImage, I can store any Image as an IImage without needing to explicitly cast. What to do? :confused: -Mike.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        MBursill wrote:

        BindingList images = new BindingList();

        Do the following :)

        BindingList<IImage> images = new BindingList<IImage>();

        **

        xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

        **

        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