用自定义比较器实例化继承的SortedDictionary的VB.NET语法

用自定义比较器实例化继承的SortedDictionary的VB.NET语法,vb.net,visual-studio-2017,icomparer,sorteddictionary,Vb.net,Visual Studio 2017,Icomparer,Sorteddictionary,以下是我的出发点:一个带有自定义比较器的SortedDictionary: Dim dict As SortedDictionary(Of Long, Object) = New SortedDictionary(Of Long, Object)(New CustomComparer()) 为了实现附加功能,我需要扩展字典,因此我现在有以下功能: Public Class CustomDict Inherits SortedDictionary(Of Long, Object) End

以下是我的出发点:一个带有自定义比较器的SortedDictionary:

Dim dict As SortedDictionary(Of Long, Object) = New SortedDictionary(Of Long, Object)(New CustomComparer())
为了实现附加功能,我需要扩展字典,因此我现在有以下功能:

Public Class CustomDict
    Inherits SortedDictionary(Of Long, Object)
End Class

Dim dict As CustomDict = New CustomDict
到目前为止,一切都很好。现在我只需要添加我的自定义比较器:

Dim dict As CustomDict = New CustomDict()(New CustomComparer())
但是编译器认为我正在尝试创建一个二维数组

结果是,如果我使用扩展SortedDictionary的类,在使用自定义比较器时会出现编译器错误,因为它认为我在尝试创建数组。我所期望的是,它会将代码识别为实例化继承SortedDictionary的类,并使用自定义比较器

总而言之,这包括:

Dim dict As SortedDictionary(Of Long, Object) = New SortedDictionary(Of Long, Object)(New CustomComparer())
虽然这会产生与二维数组相关的编译器错误:

Public Class CustomDict
    Inherits SortedDictionary(Of Long, Object)
End Class

Dim dict As CustomDict = New CustomDict()(New CustomComparer())

我的语法错了吗?或者是否有Visual Studio设置(2017 Professional)向编译器阐明我的意图?任何帮助都将不胜感激。

当继承一个类时,几乎所有的东西都会被继承,但它的构造函数除外。因此,您必须自己创建构造函数,并使其调用基类的构造函数:

Public Class CustomDict
    Inherits SortedDictionary(Of Long, Object)

    'Default constructor.
    Public Sub New()
        MyBase.New() 'Call base constructor.
    End Sub

    Public Sub New(ByVal Comparer As IComparer(Of Long))
        MyBase.New(Comparer) 'Call base constructor.
    End Sub
End Class
或者,如果您始终希望对自定义词典使用相同的比较器,则可以跳过第二个构造函数,而是让默认构造函数指定要使用的比较器:

Public Sub New()
    MyBase.New(New CustomComparer())
End Sub

@蓝白:很高兴我能帮忙!祝你的项目好运!