【问题标题】:Give a number to return the approximated value of an Enum?给出一个数字以返回 Enum 的近似值?
【发布时间】:2013-10-31 21:42:39
【问题描述】:

我有这个枚举:

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(枚举的 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 扩展的解决方案。

【问题讨论】:

  • 也许将其实现为二进制搜索,然后当您在两个数字之间时,返回最接近的那个..
  • @MikeChristensen:确实如此。如果你已经有一个排序列表,你可以使用List(Of T).BinarySearch

标签: .net vb.net linq enums numbers


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我已经适应了@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
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多