Hi,
Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue}
Color c = Color.DarkBlue;
And I want the command "Console.WriteLine(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 10244
"avivgur" <av*****@gmail.com> wrote in message
news:2c*************************@posting.google.co m... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteLine(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: newsgroupsATmettayyaDOTgotadslDOTcoDOTuk
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.google.co m... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteLine(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**************@TK2MSFTNGP10.phx.gbl>... "avivgur" <av*****@gmail.com> wrote in message news:2c*************************@posting.google.co m... Hi, Suppose I have an enum declared as:
enum Color {Red, DarkRed, Blue, DarkBlue} Color c = Color.DarkBlue;
And I want the command "Console.WriteLine(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.WriteLine() 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.WriteLine() 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...
|
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");
...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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...
| |