* "moondaddy said:
How can I create a public variable similar to an Enum, but rather than
returning integer values it would return String values?
In .NET, it's not possible to define an enumeration of any type. In some
situation, 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
'Color':
\\\
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
///