Search code examples
c#comboboxintswitch-statementtypeconverter

Convert input data to primitive data type


I faced some difficulties in converting input data in textbox to primitive data types by selecting with combobox. Below is my code, there is no error in the code but the data i input in textbox just remains the same. Please assist me in doing it the right way. Thank you.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {

  public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.Items.Add("Int");
        comboBox1.Items.Add("Double");
        comboBox1.Items.Add("Decimal");
        comboBox1.Items.Add("Float");
        comboBox1.Items.Add("String");
        comboBox1.Items.Add("Long");
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.ComboBox1_SelectedIndexChanged);
    }

    private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        calculate(((ComboBox)sender).SelectedItem.ToString());
    }

    private void calculate (string sign)
    {

        switch (sign)
        { 
            case "Int":
                System.Convert.ToInt32(textBox1);
                int.Parse(textBox1.Text);
                break;
            case "Double":
                System.Convert.ToDouble(textBox1.Text);
                double.Parse(textBox1.Text);
                break;
            case "Decimal":
                System.Convert.ToDecimal(textBox1.Text);
                decimal.Parse(textBox1.Text);
                break;
            case "Float":
                System.Convert.ToSingle(textBox1.Text);
                Single.Parse(textBox1.Text);
                break;
            case "String":
                System.Convert.ToString(textBox1.Text);
                break;
            case "Long":
                System.Convert.ToInt64(textBox1.Text);
                long.Parse(textBox1.Text);
                break;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}
}
}

Solution

  • First off, you're attempting to parse the textbox itself into these types, not the value inside the textbox. You should be using the Text property on the textbox to get the value:

    int.Parse(textBox1.Text); //Note the .Text
    

    Second, you're not assigning the result of the operation to the textbox. To assign a value to a textbox, again use the Text property:

    textBox1.Text = int.Parse(textBox1.Text).ToString();
    

    Third, this is going to fail if what is inside the textbox is not valid for the type you select. For instance, if you enter "hello" into the textbox and select int the code will crash because you can't parse "hello" into an int. You can remedy this by catching a FormatException.

    try
    {
        int.Parse(textBox1.Text);
    }
    catch (FormatException fe)
    {
        //Display an error
    }
    

    Alternatively you can use the TryParse methods on each type (int.TryParse)

    Fourth, in some instances you won't notice any thing change anyway. If you enter 123 into the textbox and parse it to an int and replace the textbox value, it will still say 123 because that's what the string representation of the int value 123 is.

    Fifth, what are you trying to accomplish here? This code basically does nothing.