How can I loop into the 2D grid decision tree in C #?

I have a list of objects on which I should set a property based on two criteria, a 2D mesh looks like this:

Property 2\Property 1   B<80        80<B<100       100<B<120           B>120
A < 100                 None          Red           Orange            Orange
100 < A < 120           None          Red           Orange             Green
120 < A < 140           None          Red           Green              Green
140 < A                 None         Orange         Green              Green

Is there a good way to create property 1 and property 2 to dynamically determine the property of an object? It is possible that in the future we will add some criteria, and I think that doing several ifs is not a good solution. I am trying to avoid redundant code.

thank

+3
source share
3 answers

2 A B 0-3:
A < 100 → Ascale = 0
, A < 120 → Ascale = 1
.. B:
B < 80 → Bscale = 0
, B < 100 → Bscale = 1
.

2d :
out = arrayname [Ascale] [Bscale];

, - , , , , - .

0

, , . ( ), :

        int[] alimits = new int[] { 100, 120, 140, int.MaxValue };
        int[] blimits = new int[] { 80, 100, 120, int.MaxValue };
        int aval = 125;
        int bval = 110;

        int aindex = 0;
        for (int i = 0; i < alimits.Count(); i++)
            if (aval < alimits[i])
                aindex = i;

        int bindex = 0;
        for (int i = 0; i < blimits.Count(); i++)
            if (bval < blimits[i])
                bindex = i;

        string[,] values = new string[,] {
            {"None", "Red", "Orange", "Orange"}, 
            {"None", "Red", "Orange", "Green"},
            {"None", "Red", "Green", "Green"},
            {"None", "Orange", "Green", "Green"}};

, . values[aindex, bindex].

+2

class AbstractSetter {void SetObjectProperty(); }

class SetterLessThen100: AbstractSettter {/* A < 100 case */}

setter, .    AbstractSetters, SetProperty() ( ).    , , AbstractSetters.

, , , , . .

0

Source: https://habr.com/ru/post/1728927/


All Articles