How can I shorten the following C # if statements?

I would like to shorten the following code, however I'm not sure where to start.

Is the switch path for this?

        static string RevisedConversionFunction(string input, string from, string to)
    {
        double exchangeRateUSD;
        double exchangeRateAUD;
        double exchangeRateCAD;
        double exchangeRateEUR;
        double exchangeRateGBP;
        double exchangeRateNZD;
        double fromExchangeRate;
        double toExchangeRate;
        exchangeRateUSD = 1;
        exchangeRateAUD = 1.31;
        exchangeRateCAD = 1.28;
        exchangeRateEUR = 0.95;
        exchangeRateGBP = 0.68;
        exchangeRateNZD = 1.36;
        fromExchangeRate = 0;
        toExchangeRate = 0;

        if (from.Equals("USD"))
        {
            fromExchangeRate = exchangeRateUSD;
        }
        if (from.Equals("AUD"))
        {
            fromExchangeRate = exchangeRateAUD;
        }
        if (from.Equals("CAD"))
        {
            fromExchangeRate = exchangeRateCAD;
        }
        if (from.Equals("EUR"))
        {
            fromExchangeRate = exchangeRateEUR;
        }
        if (from.Equals("GBP"))
        {
            fromExchangeRate = exchangeRateGBP;
        }
        if (from.Equals("NZD"))
        {
            fromExchangeRate = exchangeRateNZD;
        }

        if (to.Equals("USD"))
        {
            toExchangeRate = exchangeRateUSD;
        }
        if (to.Equals("AUD"))
        {
            toExchangeRate = exchangeRateAUD;
        }
        if (to.Equals("CAD"))
        {
            toExchangeRate = exchangeRateCAD;
        }
        if (to.Equals("EUR"))
        {
            toExchangeRate = exchangeRateEUR;
        }
        if (to.Equals("GBP"))
        {
            toExchangeRate = exchangeRateGBP;
        }
        if (to.Equals("NZD"))
        {
            toExchangeRate = exchangeRateNZD;
        }

        double amount;
        Double.TryParse(input, out amount);

        amount = (amount / fromExchangeRate) * toExchangeRate;
        amount = Math.Round(amount, 2);

        string result = Convert.ToString(amount);
        return result;
    }

I am not familiar with switches, but is there any way that they can be used in this situation?

Thanks in advance, Matt

EDIT - EDIT - EDIT - EDIT - EDIT

Thank you all for your input.

The following code is what I ended up using:

        static string RevisedConversionFunction(string input, string from, string to)
    {

        //Exchange Rates
        Dictionary<string, double> rates = new Dictionary<string, double>();
        rates.Add("USD", 1);
        rates.Add("AUD", 1.31);
        rates.Add("CAD", 1.28);
        rates.Add("EUR", 0.95);
        rates.Add("GBP", 0.68);
        rates.Add("NZD", 1.36);

        //Conversion
        double amount;
        Double.TryParse(input, out amount);
        return Convert.ToString(Math.Round(((amount / rates[from]) * rates[to]), 2));
    }
+4
source share
6 answers
  • I would use a dictionary in this case
  • use decimalinstead doubleto prevent rounding problems.

the code:

static string RevisedConversionFunction(string input, string from, string to)
{
    Dictionary<string, decimal> dExchange = new Dictionary<string, decimal>()
    { 
        {"USD" , 1},
        {"AUD" , 1.31m},
        {"CAD" , 1.28m},
        {"EUR" , 0.95m},
        {"GBP" , 0.68m},
        {"NZD" , 1.36m}
    };

    if (dExchange.ContainsKey(from) && dExchange.ContainsKey(to))
    {
        return Math.Round((decimal.Parse(input) / dExchange[from]) * dExchange[to], 2).ToString();
    }
    else
    {
        // at least one currency not in the dictionary - exception handling?
        return null; 
    }
}
+17
source

I am not a C # user, but I think it is better to use an array

var dictionary = new Dictionary<string, string>
{
    { "USD", "exchangeRateUSD" },
    { "AUD", "exchangeRateAUD" },
    { "CAD", "exchangeRateCAD" }
};
+2
source

if, ifs. , , , . ( ).

if (from.Equals("USD"))
    {
        fromExchangeRate = exchangeRateUSD;
    }
else if (from.Equals("AUD"))
    {
        fromExchangeRate = exchangeRateAUD;
    }

, , :

switch(a) { case 0: ...; break; case 1: ...; break; }

, , if..else if.., switch , .

+1

:

public static readonly Dictionary<string, double> currencyMapping = 
    new Dictionary<string, double>
    {
      {"USD",1},
      {"AUD",1.31},
      {"CAD",1.28},
      {"EUR",0.95},
      {"GBP",0.68},
      {"NZD",1.36},   
    }

, TryGet . , Dictionary, , a static readonly, , , , , const,

 double fromExchangeRate;
    if(!currencyMapping.TryGetValue(from,out fromExchangeRate))
      fromExchangeRate = <DefaultValue>
+1

.

var exchanges = new Dictionary<string, double>()
{
    ["USD"] = 1,
    ["AUD"] = 1.31,
    ["CAD"] = 1.28,
    ["EUR"] = 0.95,
    ["GBP"] = 0.68,
    ["NZD"] = 1.36,
};

double fromExchangeRate = 0;
double toExchangeRate = 0;

if (exchanges.ContainsKey(from))
{
    fromExchangeRate = exchanges[from];
}
else
{
    // 'from' not in dictionary
}


if (exchanges.ContainsKey(to))
{
    toExchangeRate = exchanges[to];
}
else
{
    // 'to' not in dictionary
}
+1

Use an array to shorten the first line of code. Arrays are always fixed in size and should be defined as follows:

double[] exchangeRate = new double[8];

// This means array is double[3] and cannot be changed without redefining it.
exchangeRate[0] = 1;
exchangeRate[1] = 1.31;
and so on..

And for String Data, save it in Array as

String[] Data = new String[8];

// This means array is double[3] and cannot be changed without redefining it.
Data[0] = "USD";
Data[1] = "AUD";
and so on..

Then use Foreach to move

foreach (String[] row in Data)
{
    if(from.Equals(Data[0]))
    {
       //Do something
    }
}

The same can be done according to your requirements.

Hope this works for you .. Thanks :-)

-1
source

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


All Articles