我不懂.我能够将我的第一个枚举值转换为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;
}
Run Code Online (Sandbox Code Playgroud)
在我的第二个案例陈述中我得到了这个投射错误:
无法隐式转换
int为PayPalTransactionType.存在显式转换(您是否错过了演员?)
Jon*_*eet 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;
}
Run Code Online (Sandbox Code Playgroud)
另外,我对未识别的代码有明确的默认操作,只是直接返回:
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
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用Dictionary<PayPalTransactionType, string>.