Let's say that I have
Code:public enum fruitType: byte { apple, orange, banana, durian, } fruitType type = fruitType.apple; Fruit myFruit = null; switch(fruitType) { case(fruitType.apple): myFruit = new Apple(); break; case(fruitType.banana): myFruit = new Banana(); break; case(fruitType.orange) myFruit = new Orange(); break; }
Above, I am trying to create a specific Fruit object in real time based on the value of type. Is there a way to do this without using a switch or an if statement?




Reply With Quote