(Aug 24, 2019 02:05 PM)CynicalCheshire Wrote: @OP I'm not sure if you have been able to get this to work but I think what may be going on is the text box has not been updated with the text input you put in your gui. That exception occurs when the input text is blank.
Code
decimal score = Convert.ToDecimal("");
the above will cause your exception.
My suggestion is two parts.
1. use the previously noted
Code
if(Double.TryParse(txtScoreTotal.Text, out double number)){
//do stuff
}
and
2. check your bindings because it appears your Text has not updated properly to contain the input you desire.
Good luck!
Edit:
to test if its your input just try to print it out and see its value or breakpoint it in debug mode.
double and decimal are different types
(Aug 22, 2019 08:29 AM)BeatMatch Wrote: no, why would i do that i was showing him an example of a bool, clearly he is new to coding, hence im not actually just using c# im also running it through a game engine and in only, wanted to use them 3 hence why i use input and not textbox.text because i have declared it somewhere else,
i already told him to try to int pars, but dont you worry about my code bud it runs smooth and does a job what i want it to do obviously there is more missing and thats just a snippet
:/ .. You wouldn't even need compiler optimization to improve code like that. An elementary student could probably optimize it by hand to produce more efficient code.
Doing this for every string representation of a number is not only going to increase conditional checks, it won't validate against arithmetic overflow.
Counting input, sum, and average is a very trivial task. What's wrong with a NumericUpDown control? Also, people who are suggesting to use the Convert class over the Parse()/TryParse() methods don't understand the difference in what these methods are doing behind the scenes.
(Aug 25, 2019 11:58 AM)unknown321 Wrote: still getting same error with the code you told me to try
If you're getting an error, you're doing something wrong at runtime with your input. What is an example input? "1234" should parse with Parse() and TryParse() with zero issues.
My guess is that you're using the wrong textbox... txtScore.Text instead? TryParse() won't throw an exception either.
One other issue here is that you're using the control's display text to continue with your calculations which is the opposite of how software is normally developed. UI is for display only, you bind data with your controls, or update it when your internal data changes. Parsing data that has been calculated and converted to a string to be displayed on the UI to be taken again, parsed back to the relevant type, and used in other expressions for calculations is wrong.