I'm working on an application that lets the user convert distances to and from inches, feet, and yards. I'm having a bit of a problem with the output. Here's my code:
private void convertButton_Click(object sender, EventArgs e)
{
int fromDistance = 0;
int toDistance = 0;
fromDistance = int.Parse(distanceConverterTextBox.Text);
string distanceInput = fromListBox.Items.ToString();
string distanceOutput = toListBox.Items.ToString();
switch (distanceInput)
{
case "Inches":
switch (distanceOutput)
{
case "Inches":
toDistance = fromDistance;
break;
case "Feet":
toDistance = fromDistance / 12;
break;
case "Yards":
toDistance = fromDistance / (3 * 12);
break;
}
break;
case "Feet":
switch (distanceOutput)
{
case "Inches":
toDistance = fromDistance * 12;
break;
case "Feet":
toDistance = fromDistance;
break;
case "Yards":
toDistance = fromDistance / 3;
break;
}
break;
case "Yards":
switch (distanceOutput)
{
case "Inches":
toDistance = fromDistance * 3 * 12;
break;
case "Feet":
toDistance = fromDistance * 3;
break;
case "Yards":
toDistance = fromDistance;
break;
}
break;
}
convertedDistanceLabel.Text = toDistance.ToString();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
And here's what the app looks like:
My control names if they help:
- The input
TextBox
:distanceConverterTextBox
- The output
Label
:convertedDistanceLabel
I've even tried not declaring the int
s to be 0, then I tried one and/or both, and the output is still zero no matter what. It can really be a simple case of accidentally using the wrong control name, but I really don't know what else to do at this point, so extra eyes will be greatly appreciated.