473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Enumeration with ToString method

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
Nov 20 '05 #1
8 1705
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" <jo*******@acxiom.com> a écrit dans le message de
news:eE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2
John,

* "John Cobb" <jo*******@acxiom.com> scripsit:
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.ToStr ing())
Dim aattr() As DescriptionAttribute = _
DirectCast( _
fi.GetCustomAttributes(GetType(DescriptionAttribut e), 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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
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)
--
Klaus H. Probst, MVP
http://www.vbbox.com/
"John Cobb" <jo*******@acxiom.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #4
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" <ya*************@hotmail.com> wrote in message
news:u%****************@TK2MSFTNGP10.phx.gbl...
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" <jo*******@acxiom.com> a écrit dans le message de
news:eE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5
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/de...matstrings.asp

Hope this helps
Jay

"John Cobb" <jo*******@acxiom.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #6
> 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.

--
Best Regards
Yanick Lefebvre
Nov 20 '05 #7
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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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/de...matstrings.asp
Hope this helps
Jay

"John Cobb" <jo*******@acxiom.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #8
John,
For custom strings I would use Herfried's method.

Hope this helps
Jay

"John Cobb" <jo*******@acxiom.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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/de...matstrings.asp

Hope this helps
Jay

"John Cobb" <jo*******@acxiom.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil | last post by:
Is it possible to override the ToString() method when creating a simple enumeration? public enum myEnum { Undefined = 0, Unassigned = 1, Assigned ...
1
by: Jamie Winder via .NET 247 | last post by:
Is it possible to iterate through all of the possible values of an enumeration? (with foreach, maybe?) What I need to do is fill a ComboBox with all possible values for an enumeration. e.g ...
1
by: Frank Moyles | last post by:
If I have an enumeration: enum SizeType { Tiny = 0, Small, Big, Massive, OMG };
3
by: DotNetNewbie | last post by:
Hi, I want to create a method that takes in the type of a Enumeration, passes in the Enumeration as an object, and then outputs the values with checkboxes, and pre-checks the checkbox if that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.