By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

Create an Enum like variable for String Contants

P: n/a
How can I create a public variable similar to an Enum, but rather than
returning integer values it would return String values?

--
mo*******@nospam.com
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Public Class MyStringEnum
Public Const ValueA As String = "First Value"
Public Const ValueB As String = "Second Value"
Public Const ValueC As String = "Third Value"
End Class

Public Class Class1

Public Sub Main()
MsgBox( MyStringEnum.ValueB )
End Sub

End Module
-Rob Teixeira [MVP]

"moondaddy" <mo*******@nospam.com> wrote in message
news:er**************@TK2MSFTNGP12.phx.gbl...
How can I create a public variable similar to an Enum, but rather than
returning integer values it would return String values?

--
mo*******@nospam.com

Nov 20 '05 #2

P: n/a
Thanks that was perfect.

--
mo*******@nospam.com
"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
Public Class MyStringEnum
Public Const ValueA As String = "First Value"
Public Const ValueB As String = "Second Value"
Public Const ValueC As String = "Third Value"
End Class

Public Class Class1

Public Sub Main()
MsgBox( MyStringEnum.ValueB )
End Sub

End Module
-Rob Teixeira [MVP]

"moondaddy" <mo*******@nospam.com> wrote in message
news:er**************@TK2MSFTNGP12.phx.gbl...
How can I create a public variable similar to an Enum, but rather than
returning integer values it would return String values?

--
mo*******@nospam.com


Nov 20 '05 #3

P: n/a
* "moondaddy" <mo*******@nospam.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.