mardi 21 août 2018

How to execute all statement except one based on some condition?

I have following method.

enum Abc
{
    A = 0,
    B = 1,
    C = 2,
    D = 3,
    E = 4,
}
int GetSum(Abc abc)
{
    int sum = 0;
    if(abc != Abc.A)
    {
        sum += 23;
    }
    if(abc != Abc.B)
    {
        sum += 68;
    }
    if(abc != Abc.C)
    {
        sum += 96;
    }
    if(abc != Abc.D)
    {
        sum += 57;
    }
    if(abc != Abc.E)
    {
        sum += 63;
    }
    return sum;
}

Here before adding value to sum I am checking whether I have to add or not based on the value of abc.

Basically, I want that if value of abc is Abc.A then execute all the statement except sum += 23;.

In my code there are too many if conditions. Is there way to do things with less if statement or any other way(better)?

Aucun commentaire:

Enregistrer un commentaire