Generics, Inheritance and Casting

AWS

Staff member
Jan 10, 2007
Florida
Given a setup like this:

public interface ISomething
{
***public void SomeMethod()
}

public class Bob : ISomething
{
***...
}

public class Jenny : ISomething
{
***...
}

public class SomethingCollection where T : ISomething
{
...
}

public SomethingCollection GimmeSomeSomethings(SomeClause)
{
***switch( SomeClause )
***{
******case One:
******return SomethingCollection

******case Two:
******return SomethingCollection
***}
}

Why doesn't this work, and how could it be made to?

main()
{
***SomethingCollection = (SomethingCollection)GimmeSomeSomethings(One)
}

(Can we assume that any typographical errors or minor issues are only because I'm writing this to illustrate the problem.)

I get a "cannot convert type SomethingCollection to SomethingCollection" error. I've been trying to read up on Covariance and Contravariance, so feel free to jargon me to death and I'll just have to cope.

Because if it doesn't work, I may have to go back and change a stack of someone's code... (whoop-ti-doo)

[edit: don't spare me, if it's simply that despite Bob*being a subclass*of (or "smaller than" if you prefer")*ISomething, SomethingCollection isn't a subclass of (smaller than) SomethingCollection. Then I need to know definitively.]

More...

View All Our Microsft Related Feeds
 
Back
Top Bottom