2

我有这个枚举:

Enum Lame_Bitrate
    kbps_8 = 8
    kbps_16 = 16
    kbps_24 = 24
    kbps_32 = 32
    kbps_40 = 40
    kbps_48 = 48
    kbps_56 = 56
    kbps_64 = 64
    kbps_80 = 80
    kbps_96 = 96
    kbps_112 = 112
    kbps_128 = 128
    kbps_144 = 144
    kbps_160 = 160
    kbps_192 = 192
    kbps_224 = 224
    kbps_256 = 256
    kbps_320 = 320
End Enum

我想返回给定数字的 Enum 的近似值。

例如,如果我有数字,190那么我希望在 Enum 中找到更近似的值以返回192(Enum 的 kbps_192 值),如果我有数字,196那么我希望再次返回该值192(不返回下一个值224因为不太近似)。

像这样的东西:

Private Sub Test()

    Dim wma_file As String = "C:\windows media audio file.wma"
    Dim wma_file_Bitrate As Integer = 172
    Dim mp3_bitrate_approximated As Integer

    mp3_bitrate_approximated = Return_Approximated_Value_Of_Enum(wma_file_Bitrate)

End Sub

private function Return_Approximated_Value_Of_Enum(byval value as integer) as integer

    return... enum.find(value).approximated...

end function

是否存在任何框架方法来找到枚举中给定其他数字的更近似的数字?

希望你能理解我的问题,谢谢。

PS:如果可以的话,我更喜欢使用 LINQ 扩展的解决方案。

4

2 回答 2

4

如果你想找到最近的枚举:

Dim number = 190
Dim allBitrates() As Lame_Bitrate = DirectCast([Enum].GetValues(GetType(Lame_Bitrate)), Lame_Bitrate())
Dim nearestBitrate = allBitrates.OrderBy(Function(br) Math.Abs(number - br)).First()

如果要查找所有最近的枚举(如果多个具有相同的最小距离):

number = 120 ' two with the same distance
Dim nearestBitrates As IEnumerable(Of Lame_Bitrate) = allBitrates.
    GroupBy(Function(br) Math.Abs(number - br)).
    OrderBy(Function(grp) grp.Key).
    First()
Console.Write(String.Join(",", nearestBitrates))

输出:

kbps_112,kbps_128
于 2013-10-31T21:50:33.933 回答
0

我已经适应了@Tim Schmelter 解决方案,并且我已经完成了 3 个通用功能。

只是我想分享这个。

· 获取最近的枚举值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Enum_Value(Of Bitrate)(133).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Enum_Value(Of KnownColor)(1000)) ' Result: 174
Private Function Get_Nearest_Enum_Value(Of T)(ByVal value As Long) As T

    Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                          .Cast(Of Object) _
                                          .OrderBy(Function(br) Math.Abs(value - br)) _
                                          .First)

End Function

· 获取最近的较低枚举值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Lower_Enum_Value(Of Bitrate)(190).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Lower_Enum_Value(Of Bitrate)(196).ToString) ' Result: kbps_192
Private Function Get_Nearest_Lower_Enum_Value(Of T)(ByVal value As Integer) As T

    Select Case value

        Case Is < [Enum].GetValues(GetType(T)).Cast(Of Object).First
            Return Nothing

        Case Else
            Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                                  .Cast(Of Object) _
                                                  .Where(Function(enum_value) enum_value <= value) _
                                                  .Last)
    End Select

End Function

· 获取最近的更高枚举值:

' Enum Bitrate As Short : kbps_128 = 128 : kbps_192 = 192 : kbps_256 = 256 : kbps_320 = 320 : End Enum
' MsgBox(Get_Nearest_Higher_Enum_Value(Of Bitrate)(196).ToString) ' Result: kbps_256
' MsgBox(Get_Nearest_Higher_Enum_Value(Of KnownColor)(1000)) ' Result: 0
Private Function Get_Nearest_Higher_Enum_Value(Of T)(ByVal value As Integer) As T

    Select Case value

        Case Is > [Enum].GetValues(GetType(T)).Cast(Of Object).Last
            Return Nothing

        Case Else

            Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)) _
                                  .Cast(Of Object) _
                                  .Where(Function(enum_value) enum_value >= value) _
                                  .First)
    End Select

End Function
于 2013-11-01T13:38:40.937 回答