473,796 Members | 2,916 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 1914
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*********@ab ilitation.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.WriteLi ne(MyEnum.OPENE D.ToString());
******* Console.WriteLi ne(MyEnum.OPENE D.ToOrdinal().T oString());
******* Console.WriteLi ne(MyEnum.INVAL ID.ToString());
******* Console.WriteLi ne(MyEnum.INVAL ID.ToOrdinal(). ToString());
******* Console.WriteLi ne(MyEnum.CLOSE D.ToString());
******* Console.WriteLi ne(MyEnum.CLOSE D.ToOrdinal().T oString());
******* Console.ReadLin e();
*** }
}

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
3773
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
1313
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 make ToString() return a
10
7141
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 how does one override the function to have some values return a different string? For example, suppose I have an enum with value for the types of DVD media like public enum DVD_Media {
6
4678
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 everything: Public Overrides Function ToString() as String, Overrides Function ToString() as String, Overrides Overloads ToString() as string, Shadows ToString() as String,
8
1731
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 Medium Large End Enum
3
3277
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 columns returned by a DataReader I would like to ensure the enumeration is always present in the child classes, but the above code wont compile
2
1109
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 have tried overriding it with the following. Public Overrides Function toString() As String Dim result As System.Text.StringBuilder Dim item As Box For Each item In Me.List result.Append(item.BoxType.ToString) Next End Function
2
1257
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 on my enumeration... but
10
105203
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
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
10468
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...
1
10205
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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
9063
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
7559
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
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 we have to send another system

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.