Jump to content

Input string is not in the correct format C#

SpaceJellyDonut
Go to solution Solved by AndySw,

1) What is the content of Value1.Text?

2) Have a look at https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx, TryParse is a bit safer :)

Hey guys! Keep getting this error and i've tried everything i could think of, any ideas?

 

 public class ResultButtonClass : MainWindow
    {
          public void ResultButtonMethod()
        {
            Variables variables = new Variables();
            //Call variables
            variables.mathMode = MathMode.Text;
            variables.value1 = Convert.ToDouble(Value1.Text); <--- Error pops up here
            variables.value2 = Convert.ToDouble(Value2.Text);

            if (variables.mathMode == "Add")
            {
                variables.result = variables.value1 + variables.value2;
                ResultBox.Text = Convert.ToString(variables.result);
            }
            else if (variables.mathMode == "Subtract")
            {
                variables.result = variables.value1 - variables.value2;
                ResultBox.Text = Convert.ToString(variables.result);
            }
            else if (variables.mathMode == "Multiply")
            {
                variables.result = variables.value1 * variables.value2;
                ResultBox.Text = Convert.ToString(variables.result);
            }
            else if (variables.mathMode == "Devide")
            {
                variables.result = variables.value1 / variables.value2;
                ResultBox.Text = Convert.ToString(variables.result);
            }

99 little bugs in the code, 99 little bugs, you take one down, you pass it around... 10335 little bugs in the code :mellow:

 

Intel Core i7 3770, ASUS Maximus IV Gene-Z, DDR3 @1866MHz 16GB (2x8GB), GIGABYTE GTX 1060 G1 Gaming 6GB

Link to comment
Share on other sites

Link to post
Share on other sites

 

Variables variables = new Variables();

Why are you storing Variables in a Variable Object?

public void ResultButtonMethod()        {            //Define variables -> may want to move elsewhere depending on which methods use this            string mathModevar;            double v1, v2;                        //Initialize variables            mathModevar = MathMode.Text;            v1 = double.parse(Value1.Text);             ETCETC
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×