Creating Enumerations of Items with a Certain Arbitrary Data Type

.NET does not allow defining enumerations of arbitrary types. In some situations an enumeration of predefined values/objects is needed. The code below creates an enumeration of type String called ClipboardType. The design follows the pattern that is used in the .NET Framework class library, for example, for the Color type:

''' <completionlist cref="ClipboardType"/>
Public Structure ClipboardType
    Private Shared ReadOnly m_Rtf As New ClipboardType("RTF")
    Private Shared ReadOnly m_Bitmap As New ClipboardType("Bitmap")
    Private Shared ReadOnly m_Text As New ClipboardType("Text")

    Public Shared ReadOnly Property Rtf() As ClipboardType
            Return m_Rtf
        End Get
    End Property

    Public Shared ReadOnly Property Bitmap() As ClipboardType
            Return m_Bitmap
        End Get
    End Property

    Public Shared ReadOnly Property Text() As ClipboardType
            Return m_Text
        End Get
    End Property

    Private Sub New(ByVal Value As String)
        m_Value = Value
    End Sub

    Private m_Value As String

    ' Alternatively, we could implement a read-only 'Value' property or
    ' something similar.
    Public Overrides Function ToString() As String
        Return m_Value
    End Function

    Public Overloads Overrides Function Equals( _
        ByVal obj As Object _
    ) As Boolean
        Return _
            DirectCast(obj, ClipboardType).ToString() = m_Value
    End Function

    ' '=' Operator overloading code goes here (Visual Basic 2005).
End Structure


Public Sub Foo(ByVal c As ClipboardType)
    MsgBox(c.ToString())    ' Get the value.
End Sub

Public Sub Test()
End Sub

IntelliSense support in Visual Studio 2005 similar to enumeration types is achieved by adding the completionlist XML comment to the class. The cref attribute takes the type whose shared members should be shown in the IntelliSense list. completionlist is undocumented but is treated correctly by the Visual Basic and C# compilers of Visual Studio 2005.