質問

I am completely new to programming, so this is probably a silly question with a simple answer. My application allows users to type a number, select what unit of measurement they are using, select what unit of measurement they what to convert to and convert it.

I have one error in the last line of code that is keeping it from working: 'Cannot implicitly convert type 'int' to 'string'. Can someone please help?

Here's the code:

   private void convertButton_Click(object sender, EventArgs e)
    {
        int fromDistance;
        int toDistance;

        fromDistance = int.Parse(distanceInput.Text);
        string measureInput = fromList.Items[fromList.SelectedIndex].ToString();
        string measureOutput = toList.Items[toList.SelectedIndex].ToString();

        switch (measureInput)
        {
            case "Yards":
                switch (measureOutput)
                {
                    case "Yards":
                        toDistance = fromDistance;
                        break;
                    case "Feet":
                        toDistance = fromDistance * 3;
                        break;
                    case "Foot":
                        toDistance = fromDistance * 3 * 12;
                        break;
                }
                break;
            case "Feet":
                switch (measureOutput)
                {
                    case "Feet":
                        toDistance = fromDistance;
                        break;
                    case "Yards":
                        toDistance = fromDistance / 3;
                        break;
                    case "Foot":
                        toDistance = fromDistance * 12;
                        break;
                }
                break;
            case "Foot":
                switch (measureOutput)
                {
                    case "Foot":
                        toDistance = fromDistance;
                        break;
                    case "Feet":
                        toDistance = fromDistance / 12;
                        break;
                    case "Yards":
                        toDistance = fromDistance / (3 * 12);
                        break;
                }
                break;
        }
        distanceOutput.Text = toDistance;
    }
役に立ちましたか?

解決

distanceOutput.Text = toDistance.ToString();

Looks like you might be used to dynamically typed languages? An int is stored in binary representation, and can represent many different values. .ToString() converts to the most common representation.

BTW you should probably not use an integer for your calculated value type, as you'll throw away fractional numbers. Use a double or float, and use Math.Round(number, decimalPoints) .

他のヒント

You can convert the input/output strings to conversion numbers by using an enum. This has the advantage of greatly simplifying your code, as well. You must, however, make sure the strings in your list exactly match the names in your enum. This simple algorithm will convert from any input to any output.

    enum Conversions
    {
        Inches = 1,
        Feet = 12,
        Yards = 36
    }

   private void convertButton_Click(object sender, EventArgs e)
    {
        int fromDistance = 0;
        int toDistance = 0;

        fromDistance = int.Parse(distanceInput.Text);

        Conversions measureinput = ((Conversions)Enum.Parse(typeof(Conversions), fromList.Items[fromList.SelectedIndex].ToString());

        Conversions measureoutput = (Conversions)Enum.Parse(typeof(Conversions), toList.Items[toList.SelectedIndex].ToString());

        toDistance = (fromdistance * (int)measureinput) / (int)measureoutput;

        distanceOutput.Text = toDistance.ToString()+ " " + Enum.GetName(typeof(Conversions), measureoutput);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top