Can't overlay 2nd Enum value on int?

I do not understand. I managed to pass the first enum int value, but not the second?

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;
}

in my second case, I get this casting error:

Cannot implicitly convert type intto PayPalTransactionType. there is an explicit conversion (are you missing a listing?)

+5
source share
10 answers

Why are you trying to quit first? Just leave this as an enumeration value everywhere:

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;
}

In addition, I will have an explicit default action for unrecognized codes and just come back directly:

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

Alternatively you can use Dictionary<PayPalTransactionType, string>.

+11
source

int ? , switch ing, !

+6

, , , , int 0 , int.

+4

?

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

, ?! enum enum, ?

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

btw - PayPalTransactionType.Authorization 0. 0 -!


,

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

...!

+2

(int) case. .

`PayPalTransactionType ', case.

+1

. case PayPalServiceBase.PayPalTransactionType.Authorization ..

0

? .

, , . , .

- :

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];
} 
0

int. .

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

- payPalTransactionType int. , :

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

switch int payPalTransactionType s. switch , .

0

,

Java/#, 2 ...

  • , int, , :

    public enum PayPalTransactionType {    (0, ),    (1, ),    (3, null),   Void (4. Null);

    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);}
    

    }

public enum TransactionType {   ( " " ),   ( " " );

;

public String getDescription()  { ();}

private TransactionType ( )  {this.description = description;} }

  1. . :

    final PayPalTransactionType payPalTransactionType = PayPalTransactionType.Authorization;// .

    payPalTransactionType.getTransactionType();//emum - , , .

0

int, . (int)PayPalServiceBase.PayPalTransactionType.Capture ( int) PayPalServiceBase.PayPalTransactionType, .

0

All Articles