Enumeration with ToString method

  • Thread starter Thread starter John Cobb
  • Start date Start date
J

John Cobb

Excuse me if I use some terminology incorrectly as I am not well versed in
Object Orientation. In short I want to create an Enumerated type similar to
the following

Enum Monitor as Byte
Small
Medium
Large
End Enum

but it also needs a a ToString method so if MyX is dimmed as Monitor I can
do both of the following:

If MyX = Monitor.Small Then
messagebox.show (MyX.ToString)
En If

I suspect I need to change this into a class somehow instead of an
enumeration but not sure how to go about it. Thanks for any help.

John
 
Hi John! :O)

look up the Enum Class in System.Reflection.

here a sample that prints "Open" base on the ConnectionState.Open
enumeration members.. :
'***
Trace.WriteLine([Enum].GetName(ConnectionState.Open.GetType(),
ConnectionState.Open))
'***
 
John,

* "John Cobb said:
Enum Monitor as Byte
Small
Medium
Large
End Enum

but it also needs a a ToString method so if MyX is dimmed as Monitor I can
do both of the following:

If MyX = Monitor.Small Then
messagebox.show (MyX.ToString)
En If

I suspect I need to change this into a class somehow instead of an
enumeration but not sure how to go about it.

My FAQ:

\\\
Imports System.ComponentModel
Imports System.Reflection

Private Enum Weekdays
<Description("Sunday.")> _
Sun
<Description("Monday.")> _
Mon
<Description("Tuesday.")> _
Tue
<Description("Wednesday.")> _
Wed
<Description("Thursday.")> _
Thu
<Description("Friday.")> _
Fri
<Description("Saturday.")> _
Sat
End Enum

Private Function GetDescription(ByVal EnumConstant As [Enum]) As String
Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
Dim aattr() As DescriptionAttribute = _
DirectCast( _
fi.GetCustomAttributes(GetType(DescriptionAttribute), False), _
DescriptionAttribute() _
)
If aattr.Length > 0 Then
Return aattr(0).Description
Else
Return EnumConstant.ToString()
End If
End Function
///

Usage:

\\\
MsgBox(GetDescription(Weekdays.Wed))
///

.... and maybe related:

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
///
 
No, no need to use a class:

Public Enum Fruits
Apple
Orange
Banana
Lime
End Enum

Dim e As Fruits = Fruits.Banana
Dim s As String = [Enum].GetName(GetType(Fruits), e)
 
I'm probably just being dense but I not following you. Can you dumb it down
a little for me? I don't see an Enum class in System.Reflection.


Zoury said:
Hi John! :O)

look up the Enum Class in System.Reflection.

here a sample that prints "Open" base on the ConnectionState.Open
enumeration members.. :
'***
Trace.WriteLine([Enum].GetName(ConnectionState.Open.GetType(),
ConnectionState.Open))
'***

--
Best Regards
Yanick Lefebvre
John Cobb said:
Excuse me if I use some terminology incorrectly as I am not well versed in
Object Orientation. In short I want to create an Enumerated type
similar
to
the following

Enum Monitor as Byte
Small
Medium
Large
End Enum

but it also needs a a ToString method so if MyX is dimmed as Monitor I can
do both of the following:

If MyX = Monitor.Small Then
messagebox.show (MyX.ToString)
En If

I suspect I need to change this into a class somehow instead of an
enumeration but not sure how to go about it. Thanks for any help.

John
 
John,
Enum already implements (an overloaded) ToString!

Dim x As Monitor
x = Monitor.Small
MessageBox.Show(x.ToString()) ' displays Small
MessageBox.Show(x.ToString("G")) ' displays Small
MessageBox.Show(x.ToString("g")) ' displays Small
MessageBox.Show(x.ToString("D")) ' displays 0 decimal
MessageBox.Show(x.ToString("d")) ' displays 0 decimal
MessageBox.Show(x.ToString("X")) ' displays 00000000 hex
MessageBox.Show(x.ToString("x")) ' displays 00000000 hex
MessageBox.Show(x.ToString("F")) ' displays Small
MessageBox.Show(x.ToString("f")) ' displays Small

For details see:

http://msdn.microsoft.com/library/d...pguide/html/cpconenumerationformatstrings.asp

Hope this helps
Jay
 
I'm probably just being dense but I not following you. Can you dumb it
down
a little for me? I don't see an Enum class in System.Reflection.

**I'm** the dumb one... The Enum class is provided by the System namespace,
sorry for the mistake.
Herfried solution is very interresting though. It let you create the
enumeration like you want and tag the members in it with a different text if
necessary. This can become very usefull in a multilingual application.
 
I don't know how I missed that. Talk about feeling stupid. One more
question, is there a way to return a custom defined value from the ToString
method? So that in the below example
x.ToString could return "13 inch monochrome" instead of "Small"? Can I do
this somehow with an Attribute similar to Flags in the Enum declaration?

Again, thanks for all the help. I'm an old VB5 guy trying to make the leap
to .Net and OOP.
 
John,
For custom strings I would use Herfried's method.

Hope this helps
Jay
 
Back
Top