2010-02-08 129 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这就是问题所在。