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. Casting Nightmare

Casting Nightmare

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

    Ok I have found that this is not possible but I wanted to know if anyone knew a way around this problem. I have an abstract item class called AbstractMyItem, from this class I implement several items, one for example is MyBox. So the code for the box is:

    public abstract class AbstractMyItem{}

    public class MyBox: AbstractMyItem{}

    For each item I want there to be a provider that performs some basic functionality for example:

    public interface IMyItemProvider{
    public AbstractMyItem [] LoadItems();
    }

    So for each I create a provider from the interface:

    public class MyBoxProvider : IMyItemProvider {
    public AbstractMyItem [] LoadItems();
    }

    However this creates a lot of casting as I have to cast the AbstractMyItem class to the MyBox class. It makes more sense to use generics like this:

    public interface IMyItemProvider<T> where T : AbstractMyItem{
    public T[] LoadItems();
    }

    public class MyBoxProvider : IMyItemProvider<MyBox>{
    public MyBox[] LoadItems();
    }

    However as I said before I need to create a list of these providers. So in theory the obvious solution is:

    List<IMyItemProvider<AbstractMyItem>> providers = new List<IMyItemProvider<AbstractMyItem>>();

    providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);

    However this will not work. You can't cast the concrete class to a the interfaced generic type. This does not work:

    IMyItemProvider<AbstractMyItem> instance = (IMyItemProvider<AbstractMyItem> ) MyBoxProvider;

    So the question. Anyone know of a good way to get around this problem? To be able to use generics with a list and assign my concrete classes to it? It would be nice to say (I know this not real code).

    List<IMyItemProvider<inherits AbstractMyItem>> providers = new List<IMyItemProvider<inherits AbstractMyItem>>();

    providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);

    J 1 Reply Last reply
    0
    • M MCEdwards

      Ok I have found that this is not possible but I wanted to know if anyone knew a way around this problem. I have an abstract item class called AbstractMyItem, from this class I implement several items, one for example is MyBox. So the code for the box is:

      public abstract class AbstractMyItem{}

      public class MyBox: AbstractMyItem{}

      For each item I want there to be a provider that performs some basic functionality for example:

      public interface IMyItemProvider{
      public AbstractMyItem [] LoadItems();
      }

      So for each I create a provider from the interface:

      public class MyBoxProvider : IMyItemProvider {
      public AbstractMyItem [] LoadItems();
      }

      However this creates a lot of casting as I have to cast the AbstractMyItem class to the MyBox class. It makes more sense to use generics like this:

      public interface IMyItemProvider<T> where T : AbstractMyItem{
      public T[] LoadItems();
      }

      public class MyBoxProvider : IMyItemProvider<MyBox>{
      public MyBox[] LoadItems();
      }

      However as I said before I need to create a list of these providers. So in theory the obvious solution is:

      List<IMyItemProvider<AbstractMyItem>> providers = new List<IMyItemProvider<AbstractMyItem>>();

      providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);

      However this will not work. You can't cast the concrete class to a the interfaced generic type. This does not work:

      IMyItemProvider<AbstractMyItem> instance = (IMyItemProvider<AbstractMyItem> ) MyBoxProvider;

      So the question. Anyone know of a good way to get around this problem? To be able to use generics with a list and assign my concrete classes to it? It would be nice to say (I know this not real code).

      List<IMyItemProvider<inherits AbstractMyItem>> providers = new List<IMyItemProvider<inherits AbstractMyItem>>();

      providers.Add((IMyItemProvider<AbstractMyItem> ) MyBoxProvider);

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      What you want to do is not possible as of C# 3. You may want to read Eric Lippert's series of articles on adding covariance and contravariance to a hypothetical version of C#[^]. In the meantime, here are some work arounds: -Use a non-generic list. -Make IMyItemProvider implement a non-generic interface, call it IProvidesItemsBase, then make your list be of type List<IProvidesItemsBase>. This will work, of course when you call LoadItems, you'd get back an array of AbstractMyItems.

      Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

      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