Domanda

I'm using Visual Studio 2008 and trying to convert this switch statement

        switch (salesOrderPayment.PaymentCardKey.ToUpper()) {
            case "MC":
                ValidateCreditCard(salesOrderPayment,errorMessages);
                break;
            case "VISA":
                ValidateCreditCard(salesOrderPayment, errorMessages);
                break;
            case "TELECHECK":
                //ValidateTelecheck(salesOrderPayment, errorMessages);
                ValidateAchCheck(salesOrderPayment, errorMessages);
                break;
            case "ACH":
                ValidateAchCheck(salesOrderPayment, errorMessages);
                break;

To use an enum that I have created

    public enum PaymentType {
        MC,
        VISA,
        AMEX,
        TELECHECK,
        CASH,
        ACH }

I've tried this:

switch (Enum.Parse(typeof(PaymentType),salesOrderPayment.PaymentCardKey.ToUpper())) 

but get red squiggly lines and when I hover over it says "A value of an integral type expected".

È stato utile?

Soluzione

Try this:

switch ((PaymentType)Enum.Parse(typeof(PaymentType),salesOrderPayment.PaymentCardKey,true))) 

Notice the cast to PaymentType type, also note that your switch cases has to be enum fields rather than strings.

I've used another overload of Enum.Parse which takes bool ignoreCase as parameter, make use of it so that you don't need ToUpper call.

Altri suggerimenti

As the Enum.Parse method returns with an Object (see here), you will need to cast the result of Enum.Parse to PaymentType.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top