1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Häufig gestellte Fragen

Erstellen von Aufzählungen mit Elementen eines beliebigen Datentyps

Erstellen von Aufzählungen mit Elementen eines beliebigen Datentyps

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 FCL, 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 stuff 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.