I have this code (the whole story behind it is here: https://codereview.stackexchange.com/questions/28990/fancy-pants-vs-cowboy-coding):
public class BeltPrinterFactory : IBeltPrinterFactory
{
public IBeltPrinter NewBeltPrinter()
{
switch (printerChoice)
{
case BeltPrinterType.ZebraQL220:
return new ZebraQL220Printer();
case BeltPrinterType.ONiel:
return new ONielPrinter();
default:
return new ZebraQL220Printer();
}
}
}
...but have added a "None" to the enum, as many customers will not have/use one:
public enum BeltPrinterType
{
None,
ZebraQL220,
ONiel
// add more as needed
}
The compiler won't allow me to not have a default case ("not all code paths return a value").
The "None" option perhaps should be the default case, but if None is the current value of printerChoice, the factory should never be called (the GUI that starts that ball rolling won't even be displayed when "None" is the value), but for the compiler's sake, how should this be implemented? Can I return nothing somehow? Or will I need to do something "weird" like:
. . .
default:
return new None();
. . .
public class None : IBeltPrinter
{
public void PrintLabel(string price, string description, string barcode)
{
;// do nothing
}
}