Hi,
Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue}
Color c = Color.DarkBlue;
And I want the command "Console.WriteL ine(c);" to print out "Dark
Blue" instead of "DarkBlue". In effect, I should probably change the
ToString() method that the enum uses to implement space insertion
between words. The question is, how do I do this?
Thanks,
Aviv. 4 10274
"avivgur" <av*****@gmail. com> wrote in message
news:2c******** *************** **@posting.goog le.com... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteL ine(c);" to print out "Dark Blue" instead of "DarkBlue". In effect, I should probably change the ToString() method that the enum uses to implement space insertion between words. The question is, how do I do this?
You cannot override ToString on an enum. You will have to write another
method to do so and call it when you want an Enum string.
In addition to Daniel's comments, you could of course create your own custom
type (and then you could override the tostring) but it would probably be
better to do what Daniel says -it would be much easier and cleaner to do.
--
Best Regards,
Mark
Mark Broadbent
mcad,mcdba,mcse +i
emailto: newsgroupsATmet tayyaDOTgotadsl DOTcoDOTuk
remove AT with '@' and DOT with '.' -please do not send spam or address will
be changed!
"avivgur" <av*****@gmail. com> wrote in message
news:2c******** *************** **@posting.goog le.com... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteL ine(c);" to print out "Dark Blue" instead of "DarkBlue". In effect, I should probably change the ToString() method that the enum uses to implement space insertion between words. The question is, how do I do this?
Thanks, Aviv.
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:<ed******* *******@TK2MSFT NGP10.phx.gbl>. .. "avivgur" <av*****@gmail. com> wrote in message news:2c******** *************** **@posting.goog le.com... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteL ine(c);" to print out "Dark Blue" instead of "DarkBlue". In effect, I should probably change the ToString() method that the enum uses to implement space insertion between words. The question is, how do I do this?
You cannot override ToString on an enum. You will have to write another method to do so and call it when you want an Enum string.
I'm not sure I understand completely...
This other method that you speak of, can it be declared as part of the
enum type to be used as "c.ToNiceString ()"? Or does it need to be a
totally seperate method to be incorporated in some helper class?
Moreover, what if I want to send the enum to Console.WriteLi ne() and
have it written correctly?... keeping in mind that WriteLine() calls
ToString()...
Thanks,
Aviv.
I blogged about a mechanism for providing alternative text for an enum. Unfortunately that one entry seems to have problems with me posting a permalink so if you follow this link http://www.dotnetconsult.co.uk/weblo...View.aspx/.NET
and scroll down the page to the two entries entitled
"Extending the humble enum" and
"Extending the humble enum (contd)"
You;ll find a technique for providing alternative enum text that used custom attributes.
Regards
Richard Blewett - DevelopMentor http://www.dotnetconsult.co.uk/weblog http://www.dotnetconsult.co.uk
I'm not sure I understand completely...
This other method that you speak of, can it be declared as part of the
enum type to be used as "c.ToNiceString ()"? Or does it need to be a
totally seperate method to be incorporated in some helper class?
Moreover, what if I want to send the enum to Console.WriteLi ne() and
have it written correctly?... keeping in mind that WriteLine() calls
ToString()...
Thanks,
Aviv. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
}
|
by: Suresh |
last post by:
Hi,
I need to add a custom ToString method on an Enum
Property. The default ToString method expands the whole
name. But, I want only an short associated code with the
long name of the enum type.
For example,
public enum Color { Red = 0, Blue, Green }
The statement
|
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
|
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
{
|
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
| |
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
|
by: Sagaert Johan |
last post by:
Hi
Since an enum member object has a ToString() method, i wonder if it can be
overriden .
johan
|
by: Tim Sprout |
last post by:
Intellisense tells me in creating the first MessageBox below that string
Enum.ToString is deprecated. Is the preferred method shown in the second
MessageBox below? What is the preferred method to convert enum to string?
private void button3_Click(object sender, EventArgs e)
{
DriveInfo d = new DriveInfo("C");
MessageBox.Show(d.DriveType.ToString());
MessageBox.Show("" + d.DriveType);
|
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.
...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |