2

I just want to know how to use the updated rate throughout the whole program. Here's my code so far for reference...

//Form 1
private void update_Click(object sender, EventArgs e)
{
    if (fromcountry.Text == tocountry.Text)
    { 
        MessageBox.Show(" Please Choose Two Different Currencies To Use This Function", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
        btnconvert.Enabled = true;
        Exchange_Rate frm = new Exchange_Rate();
        frm.Show(this);
    }
}
//Form 1 one of the comboboxes for selecting 2nd country
private void tocountry_SelectedIndexChanged(object sender, EventArgs e)
{
    btnupdate.Enabled = true;
    btnconvert.Enabled = true;
    txtvalue.Enabled = true;
    exchange();
}
private void exchange()
{
    if (fromcountry.Text == tocountry.Text)
    {
        lblexchange.Text = "1";
    }
    else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "USD - US Dollar")
    {
        lblexchange.Text = "1.26";
    }
    else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "MYR - Malaysian Ringgit")
    {
        lblexchange.Text = "2.35";
    }
    else if (fromcountry.Text == "SGD - Singapore Dollar" && tocountry.Text == "EUR - Euro")
    {
        lblexchange.Text = "0.60";
    }
//Form 2
private void btnok_Click(object sender, EventArgs e)
{
    try
    {
        double exchange;
        exchange = Double.Parse(txtcurrent.Text);
        var frm = (currencyconverter)this.Owner;
        frm.PassValue(txtcurrent.Text);
        this.Close();
    }
    catch
    {
        MessageBox.Show("Please Enter Numbers", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        txtcurrent.Text = "";
    }
}

I know by using if-else method it's too vague to get rates at the start of the program and I'm just a student learning simple programming. But still I need to know how use the updated rate when I press the same conversion again. If there's not enough info, I can help you get more coding

default locale
  • 13,035
  • 13
  • 56
  • 62
user2610573
  • 25
  • 2
  • 8

3 Answers3

4

You can use a shared currency object to hold information about rate of the currency

public class Currency
{
  private Currency(string name)
  {
    Name = name;
  }

  public string Name {get; private set;}
  public decimal Rate {get; private set;}

  private void SetRate(decimal rate)
  {
    Rate = rate;
    OnRateChanged(this);
  }

  public static event EventHandler RateCanged;
  private static OnRateChanged(Currency currency)
  {
    var handler = RateChanged;
    if(handler != null)
    {
      handler(currency, EventArgs.Empty);
    }
  }

  private Dictionary<string, Currency> currencies = new Dictionary<string, Currency>();

  public static Currency GetCurrency(string name)
  {
    Currency currency;
    if(!currencies.TryGetValue(name, out currency))
    {
      currency = new Currency(name);
      currencies[name] = currency;
    } 
  }
}

So you had a simple shared rate's storage, you can use it everywere

class Form1
{
  public Form1()
  {
    ...
    Currency.RateChanged += RateChanged;
  }

  private void RateChanged(object source, EventArgs e)
  {
    labelRate.Text = Currency.GetCurrency("USD").Rate;
  }
}

class Form2
{
  public Form2()
  {
    ...
    rateTextBox.Text = Currency.GetCurrency("USD").Rate.ToString();
  }

  void updateButtin_Click()
  {
    Currency.GetCurrency("USD").SetRate(decimal.Parse(rateTextBox.Rate));
  }  
}
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42
  • Sorry to ask if its too dry... what is that class all about and how should i link it? – user2610573 Jul 26 '13 at 09:58
  • The first piece of my code is the Currency class. This is easy currency object implementation with two fields Name and Rate. The same object used as a currencies storage which store all used curencies and inform all recepients about changes by RateCanged event – Viacheslav Smityukh Jul 26 '13 at 10:16
  • so the Name and Rate is equal to the country i selected and the rate i put it? – user2610573 Jul 26 '13 at 10:21
  • The currency class is definitely a good idea, it's something I omitted from my answer as I was trying to keep it simple but it's certainly worth considering! – Liath Jul 26 '13 at 10:42
  • how to return the onratechanged? – user2610573 Jul 26 '13 at 14:38
  • OnRateChanged used just to rise RateChanged event, you should subscribe to the RateChanged event to be notified about rate's changes. – Viacheslav Smityukh Jul 29 '13 at 05:08
2

There are a number of different ways to achieve this and it's going to be impossible to answer in full without making a design decision for you. The approaches which spring to mind are either using a configuration file, database or some external source.

As you've pointed out you need to have some way of storing these values outside your application so if an conversion rate changes you can update it in your software without rewriting your code.

You need to make a decision on how to do this.

Database A database is probably the most flexible, however it will require you to maintain it. There are countless mechanisms to access a database from ADO.NET, through Linq2SQL or NHibernate.

External Source I'm sure there are various online sources you could get currency data from, either a webservice or RSS feed you could access - it could be worth reading up on these?

Configuration Personally this is the approach I'd suggest. As you're clearly not very experienced I'd suggest the easier solution of config, work on your database skills - in the future it will be a no brainer for you.

I would use the AppSettings section of the config file similar to here.

You would add an App.Config file to your application, this would store the conversion rates so you can update them without needing to rewrite your tool. You can create a new file by right clicking on the project and adding New Item, then Configuration File.

You'll also need to add a reference onto System.Configuration as it's not referenced by default.

There is a section in the config file called AppSettings, this is a simple section for key/value type properties. We're going to create a set of app settings, one for each conversion rate. For example:

You can then use your countries to generate this key. For Example:

string settingKey = string.Concat(fromcountry.Text, "_", tocountry.Text);

You can access this configuration value using the ConfigurationManager:

decimal rate = decimal.Parse(ConfigurationManager.AppSettings[settingKey]);

Once you've got the rate you'll be able to perform your multiplication to calculate the correct values.

Please bear in mind there's no error handling in here - what happens if there country is not known or the config doesn't contain the exchange rate!

Community
  • 1
  • 1
Liath
  • 9,913
  • 9
  • 51
  • 81
  • System.Configuration I believe - the dll isn't referenced by default so you'll need to add it. – Liath Jul 26 '13 at 10:16
  • I'm very lost in using the configuration method. How should i use it? Could you elaborate more? I'm sorry to bother you any further – user2610573 Jul 26 '13 at 10:26
  • Have you used app settings before? http://stackoverflow.com/questions/10766654/appsettings-get-value-from-config-file I'll expand my answer a little but you may have to read a little around how to use the ConfigurationManager – Liath Jul 26 '13 at 10:32
  • I've added some more detail, the link in my comment about should also help you. – Liath Jul 26 '13 at 10:37
0

If you are not using actual currency data and just a static data, then here are the steps to improve:

  1. Have one currency as base currency. Usually it's USD with value 1
  2. Store all the rates for all the currencies in a collection [Key,Value] in USD. Here the Key is your Currency Code eg, SGD and value is its rate in USD.
  3. Now you can pass the selected dropdown value as Key to retrieve the value eg, Currencies[toCountry.Code]
  4. Now to get the rate. You can divide like this to get value of FromCountry in terms of ToCountry

    var FromCountryRate = Currencies[FromCountry.Value]/Currencies[ToCountry.Value];
    
Abbyjeet
  • 89
  • 5