gpt4 book ai didi

c# - 不能将第二个枚举值转换为 int?

转载 作者:太空狗 更新时间:2023-10-29 23:55:25 24 4
gpt4 key购买 nike

我不明白这个。我能够将我的第一个枚举值转换为 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;
}

在我的第二个案例陈述中,我得到了这个转换错误:

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

最佳答案

您为什么首先尝试施法?只需将其保留为枚举值即可:

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> .

关于c# - 不能将第二个枚举值转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2222106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com