Thomas
Thomas

Reputation: 69

How to replace multiple if statements i to more compact code/class in C#?

I'm having a problem converting multiple if statements (like in the example below) into more compact code. I though about tables or multidimensional arrays, but maybe you guys know a better solution. I have about 30 if statements in my code and there is no clear pattern in them in order to easily convert that code to something faster and more compact.

How can I improve this?

if (D == 0.25)
{
    if (threadPerInch == 20)
    {
        le = 0.22;
    }
    else if (threadPerInch == 28)
    {
        le = 0.25;
    }
    else if (threadPerInch == 32)
    {
        le = 0.28;
    }
    else
    {

    }
}

Upvotes: 2

Views: 2690

Answers (5)

Toshi
Toshi

Reputation: 2608

Use a Switch statement.

switch (threadPerInch)
{
    case 20:
        le = 0.22;
        break;
    case 28:
        le = 0.25;
        break;
    case 32:
        le = 0.28;
        break;
}

Or a Dictionary

Dictionary<int, decimal> threads = new Dictionary<int, decimal>()
{ 
    {20, 0.22m},
    {28, 0.25m},
    {32, 0.28m}
};

if (D == 0.25 && threads.ContainsKey(threadPerInch))
{
    le = threads[threadPerInch];
}

Upvotes: 1

Ashutosh Kumar Singh
Ashutosh Kumar Singh

Reputation: 36

If these are are hard coded values in if else statement, as mentioned by you then you can use Tuple/Dictionary to put the data and retrieve the value.

A small example of use of tuple is as below.

var population = new Tuple<string, int, int, int, int, int, int>(
                           "New York", 7891957, 7781984, 
                           7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine("Population of {0} in 2000: {1:N0}",
                  population.Item1, population.Item7);
// The example displays the following output:
//       Population of New York in 2000: 8,008,278

Thanks

Upvotes: -2

devRicher
devRicher

Reputation: 458

Yes, a very basic element of any language is the switch-case keyword. You could (not neccessary) store threadPerInch in another variable, then use switch like this:

ushort tpi = threadPerInch; //I suppose ushort, since its 20-32
switch(tpi)
{
    case 20: le = 0.22; break;
    case 28: le = 0.25; break;
    case 32: le = 0.28; break;
}

Make sure to always use break; after a statement. For additional control, use the default: keyword, which will get triggered if none of the cases are correct.

Upvotes: -1

Vyshakh Jayan
Vyshakh Jayan

Reputation: 66

You could do something like this

    Dictionary<int, double> dict = new Dictionary<int, double>();
    dict.Add(20, 0.22);
    dict.Add(28, 0.25);
    dict.Add(32, 0.28);

    le = dict[threadPerInch];

Upvotes: 0

poke
poke

Reputation: 387507

Since you only check for equality, you should use a dictionary that will essentially allow to do this lookup directly. For that, define ne mapping from threadPerInch to le as a dictionary somewhere, e.g. as a class member:

Dictionary<int, double> leForThreadPerInch = new Dictionary<int, double>() {
    [20] = 0.22,
    [28] = 0.25,
    [32] = 0.28
};

And then, you can just attempt to retrieve the value from it to get the le value:

if (D == 0.25)
{
    if (!leForThreadPerInch.TryGetValue(threadPerInch, out le))
    {
        // else case
    }
}

Upvotes: 6

Related Questions