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

有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top