Program Problem: I am trying to convert from one type of distance to the next after the user inputs the data. The user must select from one list to convert to the other list in distance. For example, selecting inches in one list to convert to yards in another list.
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Distance_Converter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ConvertButton_click(object sender, EventArgs e)
{
//int Inches = 1;
//int Feet = 12;
//int Yards = 36;
int distance_to_convert;
string lengthOption1;
string lengthOption2;
int inches_feet;
int inches_yard;
lengthOption1 = FromListBox.SelectedItem.ToString();
lengthOption2 = ToListBox.SelectedItem.ToString();
distance_to_convert = int.Parse(distancetoconvertTextBox.Text);
if ((FromListBox.SelectedIndex) != -1 && (ToListBox.SelectedIndex) != -1)
{
switch (lengthOption1)
{
case "Inches":
if (lengthOption2 == "Inches")
{
//object distancetoconvert = null;
ConvertedDistanceTextBox = distance_to_convert.ToString();
}
else if (lengthOption2 == "Feet")
{
inches_feet = distance_to_convert / 12;
ConvertedDistanceTextBox = inches_feet.ToString();
}
else if (lengthOption2 == "Yards")
{
inches_yard = distance_to_convert / 36;
ConvertedDistanceTextBox = inches_yard.ToString();
}
break;
case "Feet":
if (lengthOption2 == "Inches")
{
int feet_inches = distance_to_convert * 12;
ConvertedDistanceTextBox = feet_inches.ToString();
}
else if (lengthOption2 == "Feet")
{
ConvertedDistanceTextBox = distance_to_convert.ToString(); ;
}
else if (lengthOption2 == "Yards")
{
int feet_yard = distance_to_convert / 3;
ConvertedDistanceTextBox = feet_yard.ToString();
}
break;
case "Yards":
if (lengthOption2 == "Inches")
{
int Yards_inches = distance_to_convert * 36;
ConvertedDistanceTextBox = Yards_inches.ToString();
}
else if (lengthOption2 == "Feet")
{
int Yards_feet = distance_to_convert * 3;
ConvertedDistanceTextBox = Yards_feet.ToString();
}
else if (lengthOption2 == "Yards")
{
ConvertedDistanceTextBox = distance_to_convert.ToString(); ;
}
break;
}
}
}
private void Exitbutton_click(object sender, EventArgs e)
{
this.Close();
}
}
}
My dilemma: The code looks correct in every sense. However, when I try to convert from int to string on multiple occasions the IDE gives me a red line. The code won't compile and creates build errors. I am thinking that I will have to create a separate class to convert from int to string.
Aucun commentaire:
Enregistrer un commentaire