Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
added 90 characters in body
Source Link
Jeroen Vannevel
  • 11.6k
  • 2
  • 38
  • 79

Should you recognize this approach: http://stackoverflow.com/a/24042179/1864167


Should you recognize this approach: http://stackoverflow.com/a/24042179/1864167

Source Link
Jeroen Vannevel
  • 11.6k
  • 2
  • 38
  • 79

I would use something like this:

class Checker {
    private const byte INVENTORY = 1;
    private const byte PO = 2;
    private const byte PROMOTION = 4;
    
    public void process(int? inventory, int? po, int? promotion){
        int value = (inventory != null ? INVENTORY : 0) +
                    (po != null ? PO : 0) +
                    (promotion != null ? PROMOTION : 0);
        Process(value);
    }
    
    private void Process(int action){
        switch(action){
            case 0:
                Console.WriteLine("Nothing happens");
                break;
            case INVENTORY:
                Console.WriteLine("inventory check");
                break;
            case PO:
                Console.WriteLine("po check");
                break;
            case PROMOTION:
                Console.WriteLine("promotion check");
                break;
            case INVENTORY + PO:
                Console.WriteLine("inventorycheck = 4 & inventorycheck");
                break;

            // All other statements
                
            default: 
                throw new ArgumentException("out of bounds");
        }
    }
}

Basically it will determine the action based on a value computed by the combination of the bits representing them. It keeps it very clear what the options are and how they are handled.

An example of usage:

new Checker().process(5, null, null); // "inventory check"
new Checker().process(5, 2, null); // "inventorycheck = 4 & inventorycheck"