473,511 Members | 15,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding ToString() in an enumeration

Is it possible to override the ToString() method when
creating a simple enumeration?

public enum myEnum {
Undefined = 0,
Unassigned = 1,
Assigned = 2,
Reallocated = 3
}

I would like to be able to apply some extended features to
the ToString().

Thanks.
Nov 15 '05 #1
2 1897
You won't be able to this for an enum in C#. You could create a class with
member fields as the values and override ToString() there, however. This
would be easy and have vritually the same syntax as an Enum.

Richard

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
"Neil" <ne*********@abilitation.com> wrote in message
news:03****************************@phx.gbl...
Is it possible to override the ToString() method when
creating a simple enumeration?

public enum myEnum {
Undefined = 0,
Unassigned = 1,
Assigned = 2,
Reallocated = 3
}

I would like to be able to apply some extended features to
the ToString().

Thanks.

Nov 15 '05 #2
Here is the code again:

http://www.geocities.com/jeff_louie/OOP/oop5.htm

sealed class MyEnum
{
*** private String name;
*** private static int nextOrdinal= 1;
*** private int ordinal= nextOrdinal++;
*** private MyEnum(String name)
*** {
******* this.name= name;
*** }
*** public override String ToString()
*** {
******* return name;
*** }
*** public int ToOrdinal()
*** {
******* return ordinal;
*** }
*** public static MyEnum INVALID= new MyEnum("Invalid"); // ordinal 1
*** public static MyEnum OPENED= new MyEnum("Opened"); // ordinal 2
*** public static MyEnum CLOSED=new MyEnum("Closed"); // ordinal 3
*** /// <summary>
*** /// The main entry point for the application.
*** /// </summary>
*** [STAThread]
*** static void Main(string[] args)
*** {
******* //
******* // TODO: Add code to start application here
******* //
******* Console.WriteLine(MyEnum.OPENED.ToString());
******* Console.WriteLine(MyEnum.OPENED.ToOrdinal().ToStri ng());
******* Console.WriteLine(MyEnum.INVALID.ToString());
******* Console.WriteLine(MyEnum.INVALID.ToOrdinal().ToStr ing());
******* Console.WriteLine(MyEnum.CLOSED.ToString());
******* Console.WriteLine(MyEnum.CLOSED.ToOrdinal().ToStri ng());
******* Console.ReadLine();
*** }
}

Regards,
Jeff
Is it possible to override the ToString() method when

creating a simple enumeration?<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

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

Similar topics

2
3753
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
3
1297
by: raffelm | last post by:
Say I have an enum private enum ShippingMethods { smSnailMail, smEmail } If create var of type ShippingMethods when I call ToString() on it, I get the actual enumeration. I would like to...
10
7109
by: Ken Allen | last post by:
The ToString() function, when applied to a variable that is an enumeration type, results in a string that is the name of the enumerated value that was defined in the source code. This is cool, but...
6
4657
by: Joe | last post by:
Stupid question, but I'm really stuck I have a class that overrides ToString(). When this class is cast back to Object, and ToString() called, Object.ToString() is called instead. I've tried...
8
1714
by: John Cobb | last post by:
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...
3
3259
by: guy | last post by:
what is the best way to simulate the following line of code Public MustOverride Enum Test .... I have a data access base class and am using an enumeration in each child class to map onto the...
2
1097
by: ECathell | last post by:
I am having issues with my collection now that i have it mostly together. I am binding it to a listbox, but instead of the box information I want it to show, it is simply showing the object type. I...
2
1237
by: csharpers | last post by:
Hey, this is a very basic question for all you csharpers... I have created a class: public class Answer { public enum theAnswer { A, B, C } } I want to be able to do a toString as below...
10
104991
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
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
7367
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
7430
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...
0
7517
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...
0
5673
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,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.