Dado un número, devuelve el valor más próximo de un Enum.
#Region " Get Nearest Enum Value "
' [ Get Nearest Enum Value ]
'
' // By Elektro H@cker
'
' Examples :
'
' 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
#End Region
Dado un número, devuelve el valor próximo más bajo de un Enum.
#Region " Get Nearest Lower Enum Value "
' [ Get Nearest Lower Enum Value ]
'
' // By Elektro H@cker
'
' Examples :
'
' 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
#End Region
Dado un número, devuelve el valor próximo más alto de un Enum.
#Region " Get Nearest Higher Enum Value "
' [ Get Nearest Higher Enum Value ]
'
' // By Elektro H@cker
'
' Examples :
'
' 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).
FirstOrDefault)
End Select
End Function
#End Region
EDITO:Aquí todos juntos:
#Region " Get Nearest Enum Value "
' [ Get Nearest Enum Value ]
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(Get_Nearest_Enum_Value(Of Bitrate)(133, Enum_Direction.Nearest).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Enum_Value(Of KnownColor)(1000, Enum_Direction.Nearest)) ' Result: 174
'
' MsgBox(Get_Nearest_Enum_Value(Of Bitrate)(190, Enum_Direction.Down).ToString) ' Result: kbps_128
' MsgBox(Get_Nearest_Enum_Value(Of KnownColor)(-1, Enum_Direction.Down).ToString) ' Result: 0
'
' MsgBox(Get_Nearest_Enum_Value(Of Bitrate)(196, Enum_Direction.Up).ToString) ' Result: kbps_256
' MsgBox(Get_Nearest_Enum_Value(Of KnownColor)(1000, Enum_Direction.Up)) ' Result: 0
Private Enum Enum_Direction As Short
Down = 1
Up = 2
Nearest = 0
End Enum
Private Function Get_Nearest_Enum_Value(Of T)(ByVal value As Long, _
Optional ByVal direction As Enum_Direction = Enum_Direction.Nearest) As T
Select Case direction
Case Enum_Direction.Nearest ' Return nearest Enum value
Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)).
Cast(Of Object).
OrderBy(Function(br) Math.Abs(value - br)).
First)
Case Enum_Direction.Down ' Return nearest lower Enum value
If value < [Enum].GetValues(GetType(T)).Cast(Of Object).First Then
Return Nothing
Else
Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)).
Cast(Of Object)().
Where(Function(enum_value) enum_value <= value).
Last)
End If
Case Enum_Direction.Up ' Return nearest higher Enum value
If value > [Enum].GetValues(GetType(T)).Cast(Of Object).Last Then
Return Nothing
Else
Return [Enum].Parse(GetType(T), [Enum].GetValues(GetType(T)).
Cast(Of Object).
Where(Function(enum_value) enum_value >= value).
FirstOrDefault)
End If
End Select
End Function
#End Region