2012-08-15 184 views
-2

可能重複:
Enums returning int value獲取枚舉值

我想且具"Stack""Overflow""Stackoverflow"。因此我使用枚舉。

enum Status : int 
{ 
    Stack = 1, 
    Overflow = 2, 
    StackOverflow = 3 
} 

但是,當我想要得到堆棧的價值,我得到"Stack"

int status = 0; 
status = Status.Stack; //I want to assign 1 to status but Status.Stack returns "Stack" 

我該怎麼辦?

+1

-1是很難[谷歌](https://www.google.com/search?q=get+numeric+vslue+from+enum&rlz=1C1SKPL_enUA453UA453&sugexp =鉻,MOD = 14&的SourceID =鉻&即= UTF-8#HL = UK&pwst = 1&RLZ = 1C1SKPL_enUA453UA453&sclient = PSY-AB&q = C%23 +獲取+從+枚舉和OQ = C%23 +得到+數字+值+從+數字+值+枚舉&gs_l = serp.3..0i30.110038.111035.0.111516.3.3.0.0.0.0.124.322.1j2.3.0 ... 0.0 ... 1c.BAs9Fmuc_xg&PBX = 1&BAV = on.2,or.r_gc.r_pw.r_cp。 r_qf。&fp = 13173b19efa8ab06&biw = 1920&bih = 967)這個? – 2012-08-15 10:48:44

+0

是的,你是對的@AnatoliiG,但有一個問題:int後,它解決了..感謝您的快速回復 – 2012-08-15 10:53:28

回答

6

只是強制轉換爲int:

status = (int) Status.Stack; 
+3

我太遲了18秒+1 – Jodrell 2012-08-15 10:46:27