Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wrote public enum cmd{login=11,logout=12};
i want to use those value when i wrie string a=cmd.login it takes logout but i want login value which is 11 help me?
Posted

If you really want the numeric value associated with the Enum 'cmd converted to a 'string:
string s = Convert.ToInt32(cmd.login).ToString();
I, also, wonder why you would actually need to do this in your code; you can use Enum values in many useful ways without converting them to any other Type; for example in a Switch/Case block:
C#
cmd cmd1 = cmd.login;

switch (cmd1)
{
    case cmd.login:
        // do whatever for 'login
        break;
    case cmd.logout:
        // do whatever for 'logout
        break;
}
 
Share this answer
 
v2
C#
using System;
namespace Test
{
   public class Program
    {

       private static void Main(string[] args)
       {
           int a = (int)cmd.login;
           Console.ReadLine();
       }
    }
   public enum cmd { login = 11, logout = 12 };

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-14 1:22am    
Sorry, it does not explain anything to OP. Don't you see the total confusion of OP? You cannot just write some code dump and live it as is. As a practical code, it makes no sense at all, as some illustration... illustration of what? You don't even output anything to show OP what's going on. So why writing all that?..
—SA
midnight_ 12-Feb-14 2:15am    
" Sorry, it does not explain anything to OP. "

I don't know why you so upset, however it DOES explain something - Line 9 > Converting a enum to integer (int)cmd.login :)
When you declare an enum like that, the =11 is used to denote the value of the first item, then the rest increment from there.

Try

C#
public enum cmd{login=11,logout};
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-14 1:20am    
Sorry, this is totally equivalent OP's code, which is correct. This post is not helpful at all. The problem is different.
—SA
midnight_ 12-Feb-14 2:11am    
Its not totally equivalent... :)
Read carefully - he just puts out that you don't need to put a int after each enum, as it increments itself. (in this case)
Sergey Alexandrovich Kryukov 12-Feb-14 2:21am    
Okay, let me state is more accurately:
enum cmd{login=11,logout=12};
is totally equivalent to
enum cmd{login=11,logout};
Any doubt? Then check it up. Okay, 12 is redundant, "increament by itself" is true, so what? And, in many cases (and perhaps in OP's cases), it should really be specified explicitly. Should I even explain in what cases and why? It should be very obvious.

OP's problem is completely different, so this answer is 1) totally irrelevant to the problem; 2) provides a really bad advice, because, for some cases using auto-incrementing is good, for others it is even dangerous. If you don't know such cases, it does not mean they don't exist...

—SA
midnight_ 12-Feb-14 3:18am    
I'm sorry that I tried to challenge your mind, sensai! :)

In terms of code compiling - it's the same, indeed
In terms of writing - it's not

And that's what _Damian S_e xplained in his answer - which isn't really relevant related to this case, BUT he pointed it out right.
Sergey Alexandrovich Kryukov 12-Feb-14 10:46am    
Nothing to sorry about, thank you for your notes.
I agree that it's not the same "in terms of writing" — who would argue against the obvious?
I already explained what is bad about it. Actually, for the purpose, OP's declaration is better than in this answer.:-)
Thank you,
—SA
You did not do anything wrong (not counting ugly names violating (good) Microsoft naming conventions) in your declartion. Solutions 1 and 2 are totally misleading.

However, your second line string a=cmd.login is total gibberish and should not even compile. Why did you put it in your question?

If you need string (why?), you can use cmd.login.ToString(). If you need the value, cmd.login integer value is 11, not 12. For example if you cast it to integer it will give 11: (int)cmd.login == 11 /* will be true */. It has correct value, you just fail to see it, by one or another reason. Just look properly.

—SA
 
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