Note that there are some explanatory texts on larger screens.

plurals
  1. POInterfaces inheritance in C#
    primarykey
    data
    text
    <p>I'm trying to overrun quite big (for me) problem that I came across while writing my application. Look at this, please (I will try to shorten the code for simplicity):</p> <p>I have root interface called <code>IRepository&lt;T&gt;</code>. Next, <code>IBookRepository : IRepository&lt;Book&gt;</code> Next, concrete class that implements it: <code>BookRepository : IBookRepository</code></p> <p>In the RepositoryManager class I declared <code>private IRepository&lt;IRepoItem&gt; currentRepo;</code></p> <p><code>IRepoItem</code> is an interface that is implemented by Book class.</p> <p>Now, when I try to do something like this:</p> <pre><code>currentRepo = new BookRepository(); </code></pre> <p>VisualStudio gives error message:</p> <blockquote> <p><strong>Cannot implicitly convert type 'BookRepository' to 'IRepository'. An explicit conversion exists (are you missing a cast?)</strong></p> </blockquote> <p>I tried to cast explicitly, but runtime exception is thrown...</p> <p>I know (almost for sure) that it is something called covariance and this is (probably) solved in .Net 4.0. Unfortunately I'm writing using framework version 3.5, and I cannot change this. Please give me some advices what to do - how to overrun this problem? I'd like to get currentRepo from RepoFactory that would produce few kinds of repositories depends on user needs. I don't know whether links are allowed here, but I write some kind of blog on <a href="http://olgatherer.wordpress.com/" rel="nofollow">http://olgatherer.wordpress.com/</a> where I describe application creation. My English isn't good, but I hope that it's enough to understand me. Thank you in advance for answer.</p> <p>Best regards, skrzeczowas</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload