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.WriteLi ne(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 7350
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
(ConvertFromStr ing 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.co m
"Suresh" <an*******@disc ussions.microso ft.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.WriteLi ne(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(ConvertFromSt ring 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.co m
"Suresh" <an*******@disc ussions.microso ft.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.WriteLi ne(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 s1 = E.a.ToString( "d" ); // returns for example 'a'
string s2 = E.a.ToString( "f" ); // returns for example 'this is an enum
value a'
|
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 I have to create static functions to convert these enum values to/from
the actual comparison operator strings. As I understand it, I can't add
methods to an enum, so is there any more elegant way to do this?
|
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 "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?
|
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: 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 have an attribute which can be applied to a class definition
of a class which inherits from a base, mustinherit class. I want to define methods
in the base class which will access the contents of the attribute as it is applied 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 created new custom button called "My Custom
Button" in the Outlook menu bar.
But when I tried to create one more button called "Forward Mail" (code is
|
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 ListView columns so it should be doable, thanks
|
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 works correctly, however,
my enum is declared in a referenced assembly. The form cannot load the
enum from the referenced assembly, it indicates that it can't load the
type.
using VB, VS2005.
|
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: 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...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |