7 Replies - 879 Views - Last Post: 07 March 2012 - 10:57 AM Rate Topic: -----

#1 Fuxetan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-March 12

Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 02:02 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void pengar_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void omvandla_Click(object sender, EventArgs e)
        {
            int StringToInt = Convert.ToInt32(25);
            string fran_valuta;
            string till_valuta;
            string temp;
            double valuta1;
            double valuta2;

            fran_valuta = fran.SelectedItem.ToString();
            till_valuta = till.SelectedItem.ToString();
            temp = siffra.Value.ToString();
            valuta1 = Convert.ToDouble(temp);

            

            if (fran_valuta == "USD" && till_valuta == "SEK")
            {
                valuta2 = valuta1 * 6.72;
            }
            else if (fran_valuta == "EURO" && till_valuta == "SEK")
            {
                valuta2 = valuta1 * 9.13;
            }
            else if (fran_valuta == "GBP" && till_valuta == "SEK")
            {
                valuta2 = valuta1 * 10.68;
            }
            else if (fran_valuta == "SEK" && till_valuta == "GBP")
            {
                valuta2 = valuta1 / 10.68;
            }
            else if (fran_valuta == "SEK" && till_valuta == "USD")
            {
                valuta2 = valuta1 / 6.72;
            }
            else if (fran_valuta == "SEK" && till_valuta == "EURO")
            {
                valuta2 = valuta1 / 9.13;
            }
            else if (fran_valuta == "SEK" && till_valuta == "SEK")
            {
                valuta2 = valuta1 * 1;
            }
            else if (fran_valuta == "USD" && till_valuta == "EURO")
            {
                valuta2 = valuta1 / 9.13;
            }
            else if (fran_valuta == "USD" && till_valuta == "GBP")
            {
                valuta2 = valuta1 / 1.55;
            }
            else if (fran_valuta == "USD" && till_valuta == "USD")
            {
                valuta2 = valuta1 * 1;
            }
            else if (fran_valuta == "GBP" && till_valuta == "GBP")
            {
                valuta2 = valuta1 * 1;
            }
            else if (fran_valuta == "GBP" && till_valuta == "EURO")
            {
                valuta2 = valuta1 * 1.2;
            }
            else if (fran_valuta == "GBP" && till_valuta == "USD")
            {
                valuta2 = valuta1 * 1.6;
            }
            else if (fran_valuta == "EURO" && till_valuta == "EURO")
            {
                valuta2 = valuta1 * 1;
            }
            else if (fran_valuta == "EURO" && till_valuta == "USD")
            {
                valuta2 = valuta1 * 1.3;
            }
            else if (fran_valuta == "EURO" && till_valuta == "GBP")
            {
                valuta2 = valuta1 / 0.8;
                
                

            }
            else 
            {
                valuta2= 0; 
            }
            summa.Text = valuta2.ToString();
        }

    }

}


Any help is appriciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Got an error with my currency converter, it won't convert from GBP

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 02:44 AM

What error did you get?
Was This Post Helpful? 0
  • +
  • -

#3 Fuxetan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-March 12

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 02:57 AM

View Postnegligible, on 07 March 2012 - 02:44 AM, said:

What error did you get?


None, it doesn't do anything when i convert from GBP to any other currency.
Was This Post Helpful? 0
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 03:04 AM

Okay, I can't help you without any information. You need to make an attempt to debug your program, set up some breakpoints and follow the code through as it executes to see where it's going wrong.

http://www.dreaminco...ot-good-enough/
Was This Post Helpful? 0
  • +
  • -

#5 Fuxetan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-March 12

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 03:10 AM

View Postnegligible, on 07 March 2012 - 03:04 AM, said:

Okay, I can't help you without any information. You need to make an attempt to debug your program, set up some breakpoints and follow the code through as it executes to see where it's going wrong.

http://www.dreaminco...ot-good-enough/


Oh sorry, i thought the code would be enough when there isn't very much of it.

I am debugging it and it doesn't give me any error messages or such, that's the weird part.
Was This Post Helpful? 0
  • +
  • -

#6 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 07:02 AM

You need to show you have made some effort to solve the problem to, as debugging is a very important skill in programming.

I'm a Junior Developer and it is a good 90% of what I do every day, my most used and most useful programming skill by far.

I'd really advise you run through this debugging tutorial, following each step as it says in the thread. Then take what you have learned and apply it to your program.
http://www.dreaminco...6780-debugging/

This thread also has some very useful tips!
http://www.dreaminco...-tips-for-c%23/

In short, I could take your code and debug it and tell you the problem. However it would benefit you much more to learn how to debug properly.
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 07:34 AM

View PostFuxetan, on 07 March 2012 - 03:57 AM, said:

View Postnegligible, on 07 March 2012 - 02:44 AM, said:

What error did you get?


None, it doesn't do anything when i convert from GBP to any other currency.


The commonality there is the GBP selection. I'd bet that combobox doesn't really say "GBP". It probably says "gbp" or "gpb" and you just haven't caught the typo.




rookie What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # x. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Got an error with my currency converter, it won't convert from GBP

Posted 07 March 2012 - 10:57 AM

This is one of the problems with checking against strings.

GBP != gBP

At the very least, cast all the strings .ToLower() so you standardize the case of everything you are comparring.

Better still, use enumerations. When the From combobox has its .SelectedValue change then match that to an enum. Same with the To combobox. Then compare enums

if (fromValue == Currencies.euro && toValue == Currencies.USdollar)
{
   // blah
}
if (fromValue == Currencies.gbp && toValue == Currencies.francs)
{
  // blah
}


I'd probably go so far as to make a CurrencyExchange class that lists all the exchange rates for a specific currency in a dictionary.


class Exchange
{
   public string MyCurrency { get; set;}
   dictionary<string, float> Rates ....

   public float CalculateExchange(string From, float amount)
   {
      // Get the listing from Rates
      // Do the math
      return results;
   }
}

// Main class
list<Exchange> myExchanges = new List<Exchange>;

void PopulateExchanges()
{
   // Make new instance for France
   myExchanges.Add("GBP", 1.5f);
   myExchanges.Add("USD", 1.0f);
   // etc.


   // Make new instance for Germany
}


Once you flesh all this out, you can do away with that horrid if...else block
You just take the class matching the From, get it's .Rate value and do the math.
It would be like a 5 line method.

This post has been edited by tlhIn`toq: 07 March 2012 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1