Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
I want to create a enum like this..
C#
public enum TreeItemLevel
{
  Customer = 'CUS',
  Trip = 'TRIP'
}


Please help me
Posted
Updated 14-Jun-11 0:09am
v2
Comments
BobJanova 14-Jun-11 6:44am    
I'm guessing you don't really want to do that. Why do you think you do? What is your actual problem?

You can't. It doesn't work that way. You can't invent things and expect to be able to do them, you should read books on C# and learn what C# can do. The best way to do this, is a static class full of static strings, so tht TreeItemLevel.Customer is a string with a constant value of CUS, for example.
 
Share this answer
 
Comments
Morl99 14-Jun-11 6:57am    
Another alternative would be, using standard Enums and then map those enums to a string by using a dictionary. It depends on what you actually want to do with the string.
Sergey Alexandrovich Kryukov 14-Jun-11 11:31am    
Correct, my 5, but see my article. This should be the solution.
--SA
I've develop a whole comprehensive approach to it. Please read my article:
Human-readable Enumeration Meta-data[^].

See also related previous article:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]. You won't need a source code from this article as it is fully contained in the other one.

Good luck,
—SA
 
Share this answer
 
Comments
thatraja 14-Jun-11 12:00pm    
Wow, here comes best. 5!
BTW I forgot to check your other articles otherwise I would been answer for this question with your articles :)
Voted & Bookmarked.
Sergey Alexandrovich Kryukov 14-Jun-11 12:06pm    
Thank you very much, Raja.
It looks like you checked all my articles by now. Your attention if my pleasure.
--SA
thatraja 14-Jun-11 23:05pm    
Yes. Particularly I like your writing. BTW please let me know your soon articles too. Thank you SA. Cheers.
Anyway I'll try to publish my 1st article in this month.
Sergey Alexandrovich Kryukov 15-Jun-11 11:24am    
Well, please notify me, too.
--SA
what's about this??
MIDL
public enum TreeItemLevel
{
  CUSTOMER = "Cus",
  TRIP = "Trip"
}
 
Share this answer
 
You can refer to your enums by name if you want to, but at that point, everything becomes case-sensitive, and you lose the intellisense associated with the enums. Bad practice, and non-standard. I advise you to avoid it.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Jun-11 11:37am    
Completely agree. I devised approach free from this problem.
Please see my answer.
--SA
First of all you can not do that. If you wants to do that then do the following

public enum TreeItemLevel
   {
       [Description("CUS")]
       Customer,
       [Description("TRIP")]
       Trip
   }


Add two references System.Reflection,System.ComponentModel

Now you can access the description.

Follow the link Mapping Text to Enum entries[^]
 
Share this answer
 
Comments
Christian Graus 14-Jun-11 5:58am    
Wow - I'd almost always use my solution, but if someone had to use an enum, that's pretty cool.
Sergey Alexandrovich Kryukov 14-Jun-11 11:36am    
Major problem is that descriptions are hard-coded. You cannot use resources.
My approach is more flexible -- check it up. There are other problems solved, such as same numeric values on different enum members.
--SA
Morl99 14-Jun-11 6:55am    
thank you for bringing this up, I like this solution! It combines the great use of enums (you have an integer) with the need of Displaying some Text for that integer (without having to name the EnumVariable just the way you want it displayed). You can even add more than one Attribute, which makes it flexibel as hell. my5!
nit_singh 14-Jun-11 6:58am    
Thanks Morl99
thatraja 14-Jun-11 7:46am    
Cool, 5!
you can refer this:

String Enumerations in C#[^]

hope this helps :)
 
Share this answer
 
Comments
Christian Graus 14-Jun-11 5:57am    
Why would that help ? An enum can't do what he wants.
Uday P.Singh 14-Jun-11 6:02am    
yes! you are right, but it may helps him in understanding what enum can do
Sergey Alexandrovich Kryukov 14-Jun-11 11:32am    
Bad approach! (I did not vote.) All benefits of enumerations are lost.
Please see my answer -- and approach based on enumerations.
--SA

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