I am in my first week of programming and I created a currency converter. I want to use real time exchange rates through an api when sending the result. This code itself works just fine. But, being new I am always skeptical if it's done the proper way.
I found this method working:
var client = new WebClient(); var usdToEur = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/usd/eur"); var usdToRon = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/usd/ron"); var eurToUsd = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/eur/usd"); var eurToRon = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/eur/ron"); var ronToUsd = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/ron/usd"); var ronToEur = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/ron/eur"); Complete Method:
static double PerformConversion(string firstCurrency, string secondCurrency, double currencyValue) { var client = new WebClient(); var usdToEur = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/usd/eur"); var usdToRon = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/usd/ron"); var eurToUsd = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/eur/usd"); var eurToRon = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/eur/ron"); var ronToUsd = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/ron/usd"); var ronToEur = client.DownloadString("http://currencies.apps.grandtrunk.net/getlatest/ron/eur"); if (firstCurrency == "dollar" && secondCurrency == "euro") { return currencyValue * Convert.ToDouble(usdToEur); } if (firstCurrency == "dollar" && secondCurrency == "ron") { return currencyValue * Convert.ToDouble(usdToRon); } if (firstCurrency == "euro" && secondCurrency == "dollar") { return currencyValue * Convert.ToDouble(eurToUsd); } if (firstCurrency == "euro" && secondCurrency == "ron") { return currencyValue * Convert.ToDouble(eurToRon); } if (firstCurrency == "ron" && secondCurrency == "dollar") { return currencyValue * Convert.ToDouble(ronToUsd); } if (firstCurrency == "ron" && secondCurrency == "euro") { return currencyValue * Convert.ToDouble(ronToEur); } return currencyValue; }
WebClientinstead ofHttpClient? \$\endgroup\$