Currency Convertor Web Service

10,702

Solution 1

It's telling you clear as day... you're passing in 2 strings to you're ConversionRate(...) method when it is expecting 2 Currencys.


This seems like it might not be a WebService you are in control of, but just a consumer of...

First, the easiest way to handle consuming this WebService is to use the "Add Service Reference..." in your project (WSDL Address: http://www.webservicex.net/CurrencyConvertor.asmx?WSDL) ...

But, if you want to do it manually then create an enumeration to use and pass in an enumeration value...

public enum Currency
{
    AFA,
    ALL,
    ...
}

Convertor.ConversionRate(Currency.EUR, Currency.GBP);

Solution 2

Instead of use string "EUR" use Convertor.Currency.EUR.

Solution 3

I'm pretty new to C# and WPF so I went through the same phase as you did. Let me try to give a step by step method to make it work.

As some of the other posts said already, first you will need to add the web reference. You can do this by going to your Solution Explorer, right click on "Service References", and click "Add Service Reference". In the new window, click "Advanced" at the bottom, and in the next window click "Add Web Reference" at the bottom. Then type URL:

"http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"

Normally by now it should be looking for available services related to this URL, and find one: "CurrencyConverter". Give it a reference name such as "net.webservicex.www" and click "Add Reference". Now you can use it in your code.

Let's go to the code now. If you would like to display, for example, the Euro / US Dollar exchange rate, all you need is this code:

net.webservicex.www.CurrencyConvertor conver = new net.webservicex.www.CurrencyConvertor();
MessageBox.Show((conver.ConversionRate(net.webservicex.www.Currency.EUR, net.webservicex.www.Currency.USD)).ToString());
conver.dispose();

Hope this helps!

Solution 4

I wrote this a while back, I call the current currencies and store them in the class as the json object. This makes calculations across multiple currencies faster as you are doing on the platform. getCurrencies -> returns string[] "EUR","USD" etc calculate -> ("USD","EUR",1.0) converts 1 dollar into euros

class CurrencyConvertor
{
    public string[] currencyList;
    RestClient client = new RestClient ("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json");
    RestRequest request = new RestRequest ("",Method.GET);
    JObject json;

    public CurrencyConvertor()
    {
        var response = client.Execute(request);
        json = JObject.Parse (response.Content);
    }
    public string[] getCurrencies()
    {
        ArrayList currencies = new ArrayList ();
        foreach (var item in json["list"]["resources"]) {
            string tempN = item ["resource"] ["fields"] ["name"].ToString ().Replace ("USD/", "");
            if(tempN.Length < 4)
                currencies.Add (tempN);
        }
        currencies.Sort ();
        currencyList =  (string[])currencies.ToArray(typeof(string));

        return currencyList;
    }
    public string calculate(string Base, string Target, decimal amount)
    {
        decimal temp1 = 1;
        decimal temp2 = 1;
        Console.WriteLine (Base + "to"+Target);
        foreach (var item in json["list"]["resources"]) {
            if (item["resource"]["fields"]["name"].ToString().Contains("/"+Base)) {
                temp1 =  decimal.Parse(amount.ToString()) * decimal.Parse(item ["resource"] ["fields"] ["price"].ToString(), CultureInfo.InvariantCulture.NumberFormat);
            }
            if (item ["resource"] ["fields"] ["name"].ToString().Contains("/"+Target)) {
                temp2=decimal.Parse(amount.ToString()) * decimal.Parse(item ["resource"] ["fields"] ["price"].ToString(), CultureInfo.InvariantCulture.NumberFormat);
            }
        }
        var dec =  ((decimal)temp2 / (decimal)temp1);
        return (Math.Round(dec*amount,5) ).ToString().Replace(",",".");
    }
}
Share:
10,702
Matthew
Author by

Matthew

Updated on June 04, 2022

Comments

  • Matthew
    Matthew almost 2 years

    I am trying to make use of a currency conversion web service in my website. I have added a reference to the .asmx file.

    Here is my code:

    net.webservicex.www.CurrencyConvertor Convertor; //creating instance of web service
    
    float new_donation = donation * Convertor.ConversionRate("EUR", "GBP"); //converting donation to new value
    

    The problem is that the second line I posted is giving me the following errors:

    The best overloaded method match for 'abc.net.webservicex.www.CurrencyConvertor.ConversionRate(abc.net.webservicex.www.Currency, abc.net.webservicex.www.Currency)' has some invalid arguments

    Argument 1: cannot convert from 'string' to 'abc.net.webservicex.www.Currency'

    Argument 2: cannot convert from 'string' to 'abc.net.webservicex.www.Currency'

    Here is the link to the web service description:

    http://www.webservicex.net/ws/wsdetails.aspx?wsid=10

    How can I solve this problem? Thank you in advance.