Give a number to return the approximated value of an Enum?

Posted by ElektroStudios on Stack Overflow See other posts from Stack Overflow or by ElektroStudios
Published on 2013-10-31T21:42:39Z Indexed on 2013/10/31 21:54 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

I have this enumeration:

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

And I would like to return the approximated value of the Enum given a number.

For example, if I have the number 190 then I expect to find the more approximated value in the Enum to return the 192 (kbps_192 value of the Enum), if I have the number 196 then again I expect to return the value 192 (not return the next value 224 because is less approximated).

Something like this:

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

Exist any framework method to find the more approximated number given other number in a Enum?

I hope you can understand my question, thank you.

PS: I prefer a solution using LINQ extensions if can be.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about vb.net