Handling Enumerations

  • Thread starter Thread starter Joe H
  • Start date Start date
J

Joe H

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
 
* "Joe H said:
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?

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(ClipboardType.Rtf.ToString()) ' Get the value.
End Sub

Public Sub Test()
Foo(ClipboardType.Bitmap)
End Sub
///
 
Back
Top