Can generic SortedList do this?

Hello Everyone!

I'm sorry for my English. It's not good at all.

I have 3 classes: A, B1 and B2.

B1 and B2 inherits from A.

Then I have the following collections (VB.NET):

Dim colA As SortedList(Of String, A)
Dim colB1 As New SortedList(Of String, B1)
Dim colB2 As New SortedList(Of String, B2)

I want to to this,

colA = colB1

but the compiler does'nt let me. Why

How could I do

Thanks.



Answer this question

Can generic SortedList do this?

  • tamirms

    How could you do a workarround to solve this problem

    What sense has it got

  • Alfredo2304

    No you can't do that. The fact that B1 derives from A doesn't make SortedList(Of String, B1) a subclass of SortedList(Of String, A).



  • Skyser

    Mattias is right. You’re essentially defining two different generic classes SortedList(Of String, B1) and SortedList(Of String, A) which have no relationship to each other. The classes that you pass in after the “Of” affect what those classes act upon, but have no bearing on their actual inheritance.



    Getting this to work the way you want it to is complicated. First, you would need to subclass your lists – call them AList and B1List, which will derive from the two sorted lists respectively:



    Class AList : Inherits SortedList(Of String, A)

    Class B1List : Inherits SortedList(Of String, B1)







    Dim colA As AList

    Dim colB1 As New B1List



    Then, in either AList or B1List (doesn’t matter which), you can use operating overloading to define a conversion from the B1List to the AList; for example:



    Overloads Shared Narrowing Operator CType(ByVal bl As B1List) As AList



    And in the guts of that method you would need to enumerate over the entries in bl and copy the B1’s to A’s (inserting the result in the AList), and possibly throwing errors appropriately if there was important information in a B1 which couldn’t be held in an A (or, if you prefer, just ignore the lost information). You could also convert the other way:



    Overloads Shared Widening Operator CType(ByVal al As AList) As B1List



    And do the appropriate copying, “filling in” the additional information in the B1’s however you liked.



    After you do this, then it’s simply a matter of colA = colB1 as you’ve already done.



    This, however, is a lot of work, so you might possibly want to reconsider the definition of your lists. It sounds more to me like what you really want are lists of objects.



    --Matt Gertz--*

    VB Compiler Dev Lead

  • ianhannaford

    Thank you very much for your answer.

    This is the reason why I wanted to do that:

    Imagine that you have a class named Columns with contains 3 lists of columns:

       - 1 list for DateColumns.
       - 1 list for TextColumns.
       - 1 list for NumericColumns.

    If I try to generate the class diagram with Together for Visual Studio .NET, it can't know that there is a relationship between the class than contains the SortedList (Columns) and the other 3 classes (DateColumns, TextColumns and NumericColumns).

    That it's because the .NET Framework 1.1 version of SortedList.

    I wanted to solve this problem using Generics SortedList.

  • Can generic SortedList do this?