473,785 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1730
Hi John! :O)

look up the Enum Class in System.Reflecti on.

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

--
Best Regards
Yanick Lefebvre
"John Cobb" <jo*******@acxi om.com> a écrit dans le message de
news:eE******** ******@TK2MSFTN GP10.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*******@acxi om.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.Componen tModel
Imports System.Reflecti on

Private Enum Weekdays
<Description("S unday.")> _
Sun
<Description("M onday.")> _
Mon
<Description("T uesday.")> _
Tue
<Description("W ednesday.")> _
Wed
<Description("T hursday.")> _
Thu
<Description("F riday.")> _
Fri
<Description("S aturday.")> _
Sat
End Enum

Private Function GetDescription( ByVal EnumConstant As [Enum]) As String
Dim fi As FieldInfo = EnumConstant.Ge tType().GetFiel d(EnumConstant. ToString())
Dim aattr() As DescriptionAttr ibute = _
DirectCast( _
fi.GetCustomAtt ributes(GetType (DescriptionAtt ribute), False), _
DescriptionAttr ibute() _
)
If aattr.Length > 0 Then
Return aattr(0).Descri ption
Else
Return EnumConstant.To String()
End If
End Function
///

Usage:

\\\
MsgBox(GetDescr iption(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(Clipboar dType.Rtf.ToStr ing()) ' Get the value.
End Sub

Public Sub Test()
Foo(ClipboardTy pe.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(GetTyp e(Fruits), e)
--
Klaus H. Probst, MVP
http://www.vbbox.com/
"John Cobb" <jo*******@acxi om.com> wrote in message
news:eE******** ******@TK2MSFTN GP10.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.Reflecti on.
"Zoury" <ya************ *@hotmail.com> wrote in message
news:u%******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi John! :O)

look up the Enum Class in System.Reflecti on.

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

--
Best Regards
Yanick Lefebvre
"John Cobb" <jo*******@acxi om.com> a écrit dans le message de
news:eE******** ******@TK2MSFTN GP10.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*******@acxi om.com> wrote in message
news:eE******** ******@TK2MSFTN GP10.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.Reflecti on.


**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******** ********@TK2MSF TNGP12.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*******@acxi om.com> wrote in message
news:eE******** ******@TK2MSFTN GP10.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*******@acxi om.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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******** ********@TK2MSF TNGP12.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*******@acxi om.com> wrote in message
news:eE******** ******@TK2MSFTN GP10.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
1914
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 = 2, Reallocated = 3 }
1
246
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 foreach ( in ) { comboBox1.Items.Add (value.ToString ("G"); }
1
1677
by: Frank Moyles | last post by:
If I have an enumeration: enum SizeType { Tiny = 0, Small, Big, Massive, OMG };
3
1377
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 value is set in a mask. This is what I have so far that outputs the values as a checkbox, but I need to pass in *any* mask so it can check if that value is set in the mask, if yes, check the checkbox.
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.