Unit Converter C#

pcnThird picture pcnThird · Dec 24, 2012 · Viewed 9.9k times · Source

I have nothing else to do today so I decided to attempt to create a simple Unit Converter. So far, it's working well, but it's taking far too long due to my use of so many 'if' statements. I've searched the 'net for examples of Unit Converters made in Visual Studio, but the only one I found was a currency converter made in VS Basic that used an online converter to do the calculations. So is there a faster way to create a unit converter?

enter image description here

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem == listBox2.SelectedItem) 
        {
            txtAns.Text = txtFirstUnit.Text;
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Inches") 
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 12).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 12).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 36).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 36).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 63360).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 63360).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 5280).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 5280).ToString();
        }

        if (listBox1.SelectedItem == "Mile" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1760).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Mile")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1760).ToString();
        }

        if (listBox1.SelectedItem == "Inches" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 39.370).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Inches")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 39.370).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Feet")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 3.2808).ToString();
        }

        if (listBox1.SelectedItem == "Feet" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 3.2808).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Yard")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 1.0936).ToString();
        }

        if (listBox1.SelectedItem == "Yard" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 1.0936).ToString();
        }

        if (listBox1.SelectedItem == "Meters" && listBox2.SelectedItem == "Miles")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) * 0.00062137).ToString();
        }

        if (listBox1.SelectedItem == "Miles" && listBox2.SelectedItem == "Meters")
        {

            txtAns.Text = (double.Parse(txtFirstUnit.Text) / 0.00062137).ToString();
        }
    }
}

Answer

agent-j picture agent-j · Dec 24, 2012

An object oriented approach will get rid of the if (lst.SelectedItem) == statements.

Consider this code which has a different class for each unit. There is not an if statement to be found.

  public Form1()
  {
     InitializeComponent();

     fromList.Items.Add(new CentimeterFromMillimeterConverter());
     toList.Items.Add(new CentimeterToMillimeterConverter());
  }

  void Convert(double amount)
  {
     var from = (FromMillimeterConverter) fromList.SelectedItem;
     var to = (FromMillimeterConverter) toList.SelectedItem;
     to.Convert(from.Convert(amount));
  }

public abstract class ToMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("ToMillimeterConverter", "");
  }
}

public class CentimeterToMillimeterConverter : ToMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return 10 * centimeters;
  }
}

public abstract class FromMillimeterConverter
{
  public abstract double Convert(double unit);
  public override string ToString()
  {
     return GetType().Name.Replace("FromMillimeterConverter", "");
  }
}

public class CentimeterFromMillimeterConverter : FromMillimeterConverter
{
  public override double Convert(double centimeters)
  {
     return centimeters / 10;
  }
}