SortedDictionary键和值在某些地方使用ToArray方法,但在其他地方没有

时间:2015-02-12 15:28:54

标签: vb.net generics collections

我正在尝试对SortedDictionary个结构进行线性插值计算,如下所示: -

主程序

Imports Measurements.Measurements

Public Class Form1

Private Sub Button1_Click( sender As System.Object,  e As System.EventArgs) Handles Button1.Click

    Dim mydic As SortedDictionary(Of Distance, Temperature) =
                        New SortedDictionary(Of Distance, Temperature)
    mydic.Add(Distance.FromMetres(20), Temperature.FromCentigrade(30))
    mydic.Add(Distance.FromMetres(0), Temperature.FromCentigrade(10))

    Dim dicinterpolated_temperature As Temperature = DicInterpolate
                                                       (Distance.FromMetres(7),
                                                        mydic)
End Sub

Public Function DicInterpolate (Of YType As {Structure, IMeasurements}, _
                                   XType As {Structure, IMeasurements}) _
                  (ByVal x_value           As XType,
                   ByVal sorted_dictionary As SortedDictionary(Of XType, YType)) _
         As YType
            Return Interpolate (x_value,
                                sorted_dictionary.Keys.ToArray,
                                sorted_dictionary.Values.ToArray)
End Function

End Class

其中DistanceTemperature是实现IComparableIMeasurements等结构的结构,并在Measurements命名空间中定义。此调用调用Measurements项目中的函数,因此......

Imports System.Collections.Generic

Namespace Measurements

    Public Module Interpolation

        Public Function Interpolate (Of YType As {Structure, IMeasurements}, _
                                        XType As {Structure, IMeasurements}) _
                                    (ByVal x3 As XType,
                                     ByVal x_values() As XType, _
                                     ByVal y_values() As YType) _
                                     As YType
            ' calculate the answer and return it.
        End Function

    End Module

End Namespace

到目前为止,这么好。这一切都很好,花花公子。但是当我尝试将DicInterpolate函数推送到它真正属于的Measurements命名空间时,我在这一行上遇到了编译错误: -

Return Interpolate (x_value,
                    sorted_dictionary.Keys.ToArray,
                    sorted_dictionary.Values.ToArray)

报告的错误是: -

'ToArray' is not a member of System.Collections.Generic.SortedDictionary(Of XType, YType).KeyCollection
'ToArray' is not a member of System.Collections.Generic.SortedDictionary(Of XType, YType).ValueCollection

有谁知道为什么会这样,我能做些什么呢?

1 个答案:

答案 0 :(得分:2)

ToArraySystem.Linq.Enumerable的扩展方法,属于System.Linq命名空间。因此,您必须添加引用并在全局(在项目设置中)或在VB文件的顶部添加Imports System.Linq