Handling Enumerations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe H

    Handling Enumerations

    Enumerations are pretty useful. But the one recurring problem I have had
    with using them is the inevitably, the list of options being enumerated
    grows. Then the program has to be recompiled to account for the new
    options.

    For cases like this where the list of options can change over time, is there
    an alternative to the enumeration that captures the same functionality?

    Thanks,
    Joe


  • Herfried K. Wagner [MVP]

    #2
    Re: Handling Enumerations

    * "Joe H" <jharri@hotmail .com> scripsit:[color=blue]
    > Enumerations are pretty useful. But the one recurring problem I have had
    > with using them is the inevitably, the list of options being enumerated
    > grows. Then the program has to be recompiled to account for the new
    > options.
    >
    > For cases like this where the list of options can change over time, is there
    > an alternative to the enumeration that captures the same functionality?[/color]

    Maybe following this pattern helps:

    \\\
    Public Structure ClipboardType
    Public Shared ReadOnly Property Rtf() As ClipboardType
    Get
    Return New ClipboardType(" RTF") ' Don't cache them ;-).
    End Get
    End Property

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

    Public Shared ReadOnly Property Text() As ClipboardType
    Get
    Return New ClipboardType(" 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 'ReadOnly' '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 (VB 2005).
    End Structure
    ///

    Usage:

    \\\
    Public Sub Foo(ByVal c As ClipboardType)
    MsgBox(c.Equals (ClipboardType. Rtf))
    MsgBox(c.Equals (ClipboardType. Bitmap))
    MsgBox(Clipboar dType.Rtf.ToStr ing()) ' Get the value.
    End Sub

    Public Sub Test()
    Foo(ClipboardTy pe.Bitmap)
    End Sub
    ///

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>

    Comment

    Working...