On Jul 15, 9:34*am, "Pieter" <pieterNOSPAMcou...@hotmail.com> wrote:
> I have a class clsSubClass which inherits from clsClass.
>
> When I instantiate an object of clsClass (MyClass), and I instantiate an
> object from clsSubclass (MySubClass) I can do an "MyClass = MySubclass"..
>
> But when I declare two generic list of them ("Dim MyList1 as List(Of
> clsClass)" and "Dim MyList2 as List(Of clsSubClass)"), I can't do an
> "MyList1 = MyList2".
Indeed. That's because generics don't exhibit variance.
> Why is this exactly, and is there a way to implement this behaviour?
Brief answer: consider this code.
List<Banana> bananaBunch = new List<Banana>();
List<Fruit> fruitbowl = bananaBunch;
fruitbowl.Add(new Apple());
Suppose this were legal - then bananaBunch would contain an Apple,
which is clearly invalid.
For a lot of detail, see Eric Lippert's series of blog articles:
http://blogs.msdn.com/ericlippert/ar...e/default.aspx
Jon