Input string was not in a correct format

后端 未结 8 1347
滥情空心
滥情空心 2020-11-22 02:55

I\'m new with C#, I have some basic knowledge in Java but I can\'t get this code to run properly.

It\'s just a basic calculator, but when I run the program VS2008 gi

8条回答
  •  感动是毒
    2020-11-22 03:27

    The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.

    It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.

提交回复
热议问题