2010-02-08 86 views
5

我不明白這一點。我能夠將我的第一枚枚舉值賦予int而不是第二枚呢?不能將第二枚枚舉值轉換爲int?

public enum PayPalTransactionType 
{ 
    Authorization = 0, // Debit 
    Capture = 1, // Credit 
    Refund = 2, 
    Void = 3 
} 

public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    string actionCode = string.Empty; 

    switch (payPalTransactionType) 
    { 
     case (int)PayPalServiceBase.PayPalTransactionType.Authorization: 
      actionCode = "Debit"; 
      break; 
     case (int)PayPalServiceBase.PayPalTransactionType.Capture: 
      actionCode = "Credit"; 
      break; 
    } 

    return actionCode; 
} 

在我的第二個case語句,我得到這個錯誤鑄造:

Cannot implicitly convert type int to PayPalTransactionType . An explicit conversion exists (are you missing a cast?)

+0

請編輯您的問題,將所有的代碼放在代碼塊中,現在很混亂。謝謝 – 2010-02-08 14:18:19

+0

@Marcel:完成 – AakashM 2010-02-08 14:19:32

+2

爲什麼在你的case語句中將'enum'強制轉換爲'int'?實際上你根本不需要施展這些。 – 2010-02-08 14:22:12

回答

11

你爲什麼試圖擺在首位?只要把它作爲枚舉值無處不在:

public string GetPayPalTransCode 
    (PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    string actionCode = string.Empty; 

    switch (payPalTransactionType) 
    { 
     case PayPalServiceBase.PayPalTransactionType.Authorization: 
      actionCode = "Debit"; 
      break; 
     case PayPalServiceBase.PayPalTransactionType.Capture: 
      actionCode = "Credit"; 
      break; 
    } 

    return actionCode; 
} 

此外,我不得不爲無法識別碼明確的默認操作,而直接返回:

public string GetPayPalTransCode 
    (PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    switch (payPalTransactionType) 
    { 
     case PayPalServiceBase.PayPalTransactionType.Authorization: 
      return "Debit"; 
     case PayPalServiceBase.PayPalTransactionType.Capture: 
      return "Credit"; 
     default: 
      return ""; // Or throw an exception if this represents an error 
    } 
} 

或者,你可以使用一個Dictionary<PayPalTransactionType, string>

2

爲什麼在上帝的份上,你在做類型轉換?

public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    switch (payPalTransactionType) 
    { 
     case (int)PayPalServiceBase.PayPalTransactionType.Authorization: 
      break; 
     case (int)PayPalServiceBase.PayPalTransactionType.Capture: 
      break; 
    } 
} 

bascially相同類型在這裏,不是嗎?!你想比較enum和enum,不是嗎?

只是做

public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    // ... 
    switch (payPalTransactionType) 
    { 
     case PayPalServiceBase.PayPalTransactionType.Authorization: 
      break; 
     case PayPalServiceBase.PayPalTransactionType.Capture: 
      break; 
    } 
    // ... 
} 

順便說一句 - 這不是分配給PayPalTransactionType.Authorization0最佳實踐。 0應該用於解析回退!

編輯:如果你

public string GetPayPalTransCode(PayPalServiceBase.PayPalTransactionType payPalTransactionType) 
{ 
    switch ((int)payPalTransactionType) 
    { 
     case (int)PayPalServiceBase.PayPalTransactionType.Authorization: 
      break; 
     case (int)PayPalServiceBase.PayPalTransactionType.Capture: 
      break; 
    } 
} 

這是相當
您的代碼將是正確的......!

0

您並不需要這些演員擺在首位。只是讓它case PayPalServiceBase.PayPalTransactionType.Authorization

6

你爲什麼要選擇int?你在switch上的東西已經是枚舉類型了!

0

你爲什麼要把它放在第一位。只需使用枚舉類型執行case語句。

如果你真的只是查找一個字符串,更好的方法將是一個字符串數組,你可以通過枚舉類型進行索引。這會更快,代碼更少,維護也更容易。

因此,像這樣:

public enum PayPalTransactionType 
{ 
    Authorization = 0, // Debit 
    Capture = 1, // Credit 
    Refund = 2, 
    Void = 3, 
    Max // This must always be the last type and is used just as a marker. 
} 
string[] ActionCode = new string[(int)PayPalTransactionType.Max] { "Debit", "Credit", "Refund", "Void" }; 


public string GetPayPalTransCode(PayPalTransactionType payPalTransactionType) 
{ 
    return ActionCode[(int)payPalTransactionType]; 
} 
1

取出(int)投射在你的case語句。該開關可以處理枚舉值。

由於開關處於「PayPalTransactionType」狀態,因此應在案例語句中使用該類型的值。

4

至於問題的其他部分,第一次強制轉換的原因是因爲從常量int 0到enum類型的隱式轉換總是有效,而另一個嘗試轉換則來自非零int值。

0

你不需要演員到int。直接使用枚舉。

switch (payPalTransactionType) 
{ 
    case PayPalServiceBase.PayPalTransactionType.Authorization: 
     actionCode = "Debit"; 
     break; 
    case PayPalServiceBase.PayPalTransactionType.Capture: 
     actionCode = "Credit"; 
     break; 
} 

錯誤的發生是因爲你的努力隱式轉換payPalTransactionTypeint。也許這將啓發你的問題:

switch ((int)payPalTransactionType) 
{ 
    case (int)PayPalServiceBase.PayPalTransactionType.Authorization: 
     actionCode = "Debit"; 
     break; 
    case (int)PayPalServiceBase.PayPalTransactionType.Capture: 
     actionCode = "Credit"; 
     break; 
} 

這第一投可以讓你switchint!而非PayPalTransactionType秒。由於switch語句支持枚舉,所以不需要這種轉換。

0

添,

我不是一個Java/C#專家,但這裏是我的2上面的代碼塊美分...

  1. 如果您使用的是枚舉,爲什麼你需要有一個int值,如果你這樣做,那麼這樣做:

    公共枚舉PayPalTransactionType { 授權(0,借記卡), 捕捉(1,信用), 退款(3,NULL) Void(4。空值);

    private final int code; 
    
    public int getCode() 
    {return(code);} 
    
    private PayPalTransactionType(final int code, final TransactionType transactionType) 
    { 
        this.code = code; 
        this.transactionType = transactionType; 
    } 
    
    private TransactionType getTransactionType() 
    {return(transactionType);} 
    

    }

公共枚舉TRANSACTIONTYPE { 積分( 「資金將被添加到指定的帳戶。」), 借記( 「資金從指定的帳戶扣除。」);

private final String description;

公共字符串getDescription() {返回(介紹);}

私人TRANSACTIONTYPE(最終字符串描述) {this.description =描述;}}

  • 交換機適用於極少數情況。你可以簡單地這樣做:

    最終PayPalTransactionType payPalTransactionType = PayPalTransactionType.Authorization; //這只是你會傳入參數

    payPalTransactionType.getTransactionType()的一個實例; //一個emum是一個靜態數據庫,只需存儲與此值相關的其他內容。

  • 沃爾特

    0

    這不是爲int是傳給你一個問題投。它的(int)PayPalServiceBase.PayPalTransactionType.Capture(這是一個int)轉換回PayPalServiceBase.PayPalTransactionType這就是問題所在。