Unit Converter C#
10,041
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;
}
}
Related videos on Youtube
Author by
pcnThird
Interesting thoughts: The wise man acknowledges his foolishness.
Updated on September 14, 2022Comments
-
pcnThird over 1 year
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?
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(); } } }
-
Mitch Wheat over 11 years"is there a faster way to create a unit converter?" - Yes, it's called a lookup table...
-
Maurice Reeves over 11 yearsI know this isn't a production system, or something you're necessarily release to the public, but you should definitely be checking your inputs to make sure they're sane.
-
-
pcnThird over 11 yearsThanks. I was viewing my problem at a very basic level, but I have to use some more advanced techniques if I want to become proficient in C#. I usually try to take the easiest way, but it's too time-consuming most of the time.
-
agent-j over 11 yearsBest of luck.
if/switch
statements are usually troublesome unless you are certain there won't be yet anotherelse/case
. I hope this gives you some new ideas.