SortedDictionary 键和值在某些地方有 ToArray 方法,但在其他地方没有
SortedDictionary Keys and Values have ToArray method in some places, but not others
我正在尝试对 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
其中 Distance
和 Temperature
是实现 IComparable
和 IMeasurements
等的结构,并在 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
有谁知道为什么会这样,我能做些什么?
ToArray
is an extension method of System.Linq.Enumerable
属于 System.Linq
命名空间。因此,您必须添加引用并在全局(在项目设置中)或在 VB-File 的顶部添加 Imports System.Linq
。
我正在尝试对 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
其中 Distance
和 Temperature
是实现 IComparable
和 IMeasurements
等的结构,并在 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
有谁知道为什么会这样,我能做些什么?
ToArray
is an extension method of System.Linq.Enumerable
属于 System.Linq
命名空间。因此,您必须添加引用并在全局(在项目设置中)或在 VB-File 的顶部添加 Imports System.Linq
。