C Sharp

Combining Case Labels

In the Payment example, we used a different case label for each possible evaluation of the Payment.tender field. However, what if you want to combine case labels? For example, you might want to display a credit card authorization dialog box for any of the three credit card types deemed valid in the Tenders enum. In that case, you could place the case labels one right after the other, like so: -

using System;
enum Tenders : int
{
    Cash = 1,
    Visa,
    MasterCard,
    AmericanExpress
};
class Payment
{
    public Payment(Tenders tender)
    {
        this.Tender = tender;
    }
    protected Tenders tender;
    public Tenders Tender
    {
        get
        {
            return this.tender;
        }
        set
        {
            this.tender = value;
        }
    }
    public void ProcessPayment()
    {
        switch ((int)(this.tender))
        {
            case (int)Tenders.Cash:
                Console.WriteLine
                           ("\nCash - Everyone's favorite tender.");
                break;
            case (int)Tenders.Visa:
            case (int)Tenders.MasterCard:
            case (int)Tenders.AmericanExpress:
                Console.WriteLine
("\nDisplay Credit Card Authorization Dialog.");
                break;
            default:
                Console.WriteLine("\nSorry - Invalid tender.");
                break;
        }
    }
}
class CombiningCaseLabelsApp
{
    public static void Main()
    {
        Payment payment = new Payment(Tenders.MasterCard);
        payment.ProcessPayment();
    }
}

If you instantiate the Payment class with Tenders.Visa, Tenders.MasterCard, or Tenders.AmericanExpress, you'll get this output: -

Display Credit Card Authorization Dialog.

by BrainBellupdated
Advertisement: