Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i am facing a problem to access enum values.

i made an enum

enum caroptions
{
maruti alto,
swift lx,
xylo xuv
};
but while i am acessing values in intellisense is coming
for maruti alto is only maruti
and swift lx is only swift.

for all values after space is not coming.

i am accessing like on button click event

caroptions option = option.(name of values inside enum)

please help me.
Posted
Updated 2-Jan-14 18:23pm
v2

As I am sure you realize by now, Enum labels cannot use white-space, but, there is a way you can associate descriptive text with each Enum element:
C#
// requires: using System.ComponentModel;

enum caroptions
{
    [Description("maruti alto")]
    maruti,
    [Description("swift lx")]
    swift,
    [Description("xylo xuv")]
    xylo
}
However, accessing that descriptive text at run-time requires using System.Reflection; do you really want to be paying the price of using code like this:
C#
FieldInfo fi = caroptions.maruti.GetType().GetField(caroptions.maruti.ToString());

string description = ((DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false))[0].Description;
Yes, of course you could package that code up in a method, or make it into an extension method, and reduce its verbosity, but you are still doing some heavy-lifting, imho, for a small pay-off.
 
Share this answer
 
hi try this


C#
enum caroptions
 {
 maruti_alto,
 swift_lx,
 xylo_xuv
 };

caroptions option = option.maruti_alto;


see MSDN[^]
 
Share this answer
 
v2
Comments
TrushnaK 3-Jan-14 0:44am    
right....
don't give space in your enum

refer this:- [^]
 
Share this answer
 
Hi Bunty,

Please go through these links
Link1[^]

Link2[^]
 
Share this answer
 
try like this..

C#
enum caroptions
{
maruti =1,
swift =2,
xylo= 3
};


we can assign only integral value to the enum.
dont give space to it.

or try as Bojjaiah posted.

have a look on enums from here.
enums[^]
enums[^]
Enums in C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900