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
Color cr = Color.Red;
Console.WriteLine(cr.ToString());
prints, "Red". But, I want to override ToString of Color
to yeild a result "RD" for red. "BL" for Blue and "GR" for
green.
Can anyone help me out?
Thanks and Best Regards
Suresh. 2 7330
Suresh,
You are not going to be able to do this. The reason for this is that
you can not extend structures so you can not override the ToString method.
What I would do is create a class that derives from TypeConverter, and
then override the ConvertFrom and ConvertTo methods. You can then transform
your color to your code based the color passed in. You would then
instantiate an instance of this class and then call the appropriate methods
(ConvertFromString and ConvertToString, these methods will end up calling
the ConvertFrom and the ConvertTo methods).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:07****************************@phx.gbl... 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
Color cr = Color.Red; Console.WriteLine(cr.ToString());
prints, "Red". But, I want to override ToString of Color to yeild a result "RD" for red. "BL" for Blue and "GR" for green.
Can anyone help me out?
Thanks and Best Regards Suresh.
Thanx Nich.
That exactly solved my problem.
Instead I used EnumCoverter class.
Best Regards
Suresh. -----Original Message----- Suresh,
You are not going to be able to do this. The reason
for this is thatyou can not extend structures so you can not override the
ToString method. What I would do is create a class that derives from
TypeConverter, andthen override the ConvertFrom and ConvertTo methods. You
can then transformyour color to your code based the color passed in. You
would theninstantiate an instance of this class and then call the
appropriate methods(ConvertFromString and ConvertToString, these methods
will end up callingthe ConvertFrom and the ConvertTo methods).
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Suresh" <an*******@discussions.microsoft.com> wrote in
messagenews:07****************************@phx.gbl... 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
Color cr = Color.Red; Console.WriteLine(cr.ToString());
prints, "Red". But, I want to override ToString of Color to yeild a result "RD" for red. "BL" for Blue and "GR"
for green.
Can anyone help me out?
Thanks and Best Regards Suresh.
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Wiktor Zychla |
last post by:
I would like to be able to convert enums to their string representation but
I would like to be able to apply a custom formatting, so that I could write
for example:
enum E { a, b, c };
string...
|
by: John Smith |
last post by:
I have a custom enum to list comparison operators: =, <, <=, >, >= . I have
created it as follows:
public enum Comparator {Equal, LessThan,
LessThanOrEqual,GreaterThan,GreaterThanOrEqual}
Now...
|
by: avivgur |
last post by:
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...
|
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...
|
by: Jeff Mason |
last post by:
Hi,
I'm having a reflection brain fog here, perhaps someone can set me on the right
track.
I'd like to define a custom attribute to be used in a class hierarchy.
What I want to do is to...
|
by: Kannan |
last post by:
Hi,
I am trying to created Outloook Add-in Com in outlook using C#.
I have seen this URL for developing this sample
http://support.microsoft.com/?kbid=302901
When I executed this program it...
|
by: Pete Kane |
last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would
contain tab pages of my own classes, I know you can achieve this with...
|
by: filipk69 |
last post by:
Could someone point me in the right direction, please.
I have an enum something like this:
enum TextFieldType
{
FieldABC = 3000,
FieldXYZ = 3001,
…..
…..
}
|
by: Rick |
last post by:
Hi group,
I'm trying to inherit from the windows checkbox control. This is no
problem. My goal is to add a property that is based on an enum. When I
have an enum declared within the class, it...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |