mercredi 10 octobre 2018

Alternative to if/else on combinational logic in C#

I am working on a program that uses a grid system. The system needs a method that works on every element of the grid, based on the value of its neighbours' elementType. What I currently use is something along the lines of the follows:

enum ElementType{
    A,B
}

if (neighbourUp.elemType == ElementType.A && neighbourDown == ElementType.A){
    method1();
}

if (neighbourLeft == ElementType.A and current == ElementType.B){
    method2();
}

and so on. As you can see this is hard to manage when the types increase. Ideally I would like to use polymorphism here but I feel creating a class for each combination is too much work. Also there may be cases where the method for some combinations is the same.

I would like some advice on how to approach this. Also the ElementType needs to be expandable to accommodate new types that may get added later.

Aucun commentaire:

Enregistrer un commentaire