Results 1 to 2 of 2

Thread: Get Constituent Values of a Combined Enumerated Value

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Get Constituent Values of a Combined Enumerated Value

    The following code was inspired and co-written by FourBlades.

    The following method will take any combined enumerated value and return an array containing the constituent parts. It is intended to be used with enumerated types that have the Flags attribute applied, allowing you to combine multiple values into a single value, where each bit of the composite value represents one of the flags. An example of such a type is the FontStyle enumeration.
    VB.NET Code:
    1. Public Function GetConstituentFlags(Of T)(ByVal value As T) As T()
    2.     Dim genericType As Type = GetType(T)
    3.  
    4.     If Not genericType.IsEnum Then
    5.         Throw New ArgumentException("Generic argument type must be an enumerated type.")
    6.     End If
    7.  
    8.     Dim numericValue As Integer = Convert.ToInt32(value)
    9.     Dim flags As New List(Of T)
    10.  
    11.     If numericValue = 0 Then
    12.         If [Enum].IsDefined(genericType, 0) Then
    13.             flags.Add(DirectCast([Enum].ToObject(genericType, 0), T))
    14.         End If
    15.     Else
    16.         For Each constant As Integer In [Enum].GetValues(genericType)
    17.             If constant <> 0 AndAlso (numericValue And constant) = constant Then
    18.                 flags.Add(DirectCast([Enum].ToObject(genericType, constant), T))
    19.             End If
    20.         Next constant
    21.     End If
    22.  
    23.     Return flags.ToArray()
    24. End Function
    This is a good example of a generic function. Many people will only be familiar with generics in relation to collections. If you want to see it in action, try editing the Bold, Italic, Underline and Strikethrough properties of a form's Font property and running the project with this code:
    VB.NET Code:
    1. Private Sub Form1_Load(ByVal sender As Object, _
    2.                        ByVal e As EventArgs) Handles MyBase.Load
    3.     For Each style As FontStyle In Me.GetConstituentFlags(Me.Font.Style)
    4.         MessageBox.Show(style.ToString())
    5.     Next style
    6. End Sub
    Note that if you don't apply any styles to the font the method automatically returns the zero value for that enumeration, which is Regular. I haven't specifically tested it but this method should also support negative values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: Get Constituent Values of a Combined Enumerated Value

    The following method, a variation on the method in post#1, will take any combined or single enumerated value as an instance of any Enumeration and return an array containing the both the lowest and highest value found in the given Enum Type. It is intended to be used with enumerated types.

    In this function, the following line of code uses GetValues to return An Array of the values of the constants in value (ByVal value As T).

    Code:
    Dim values() As Integer = CType([Enum].GetValues(genericType), Integer())
    The elements of the return array are sorted by the values of the enumeration constants; therefore, sorting is not required to ensure that the subsequent lines of code correctly return the lowest & highest (in that order) enum values found in the Type represented by value (ByVal value As T).

    The full Function is:

    Code:
            Public Shared Function GetLowAndHighEnumValues(Of T)(ByVal value As T) As T()
    
                Dim genericType As Type = GetType(T)
                If Not genericType.IsEnum Then
                    Throw New ArgumentException("Argument 'T' must be an enumerated type. " & genericType.FullName & " is not a valid parameter for function 'GetLowAndHighEnumValues' ")
                End If
    
                Dim flags As New List(Of T)
                Dim values() As Integer = CType([Enum].GetValues(genericType), Integer())
                flags.Add(DirectCast([Enum].ToObject(genericType, values(0)), T))
                flags.Add(DirectCast([Enum].ToObject(genericType, values(values.GetUpperBound(0))), T))
    
                Return flags.ToArray
    
            End Function

    An example of the usage of this function is:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim styles() As FontStyle = GetLowAndHighEnumValues(FontStyle.Bold)
            For Each style As FontStyle In styles
                MessageBox.Show(style.ToString)
            Next
    
        End Sub
    Last edited by FourBlades; Jan 1st, 2008 at 02:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width