Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top