Anyone let me know what is going on here? Think it is something to do with the scope.

double answer;

            if (sender is System.Windows.Forms.Button)
            {
                try
                {
                    answer = Convert.ToDouble(answerBox.Text);//if cannot convert 
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Enter a valid number" + ex.Message(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
           
            setPicsToVisible();//this method sets pictures to non visible

            if (currentUser.numQuestionsComplete < 10)
            {
                checkAnswer(setValues, answer);
                currentUser.numQuestionsComplete++;
                determineShape();//get shape to be
            }

Now when the first if statement round the try catch block is removed, no errors show to me. If i keep the if statement in, i get an error that i do not assign any value to 'answer' which is annoying because i don't see how one if statement is stopping me from doing this?

You want to convert some value to double, which apparently is NOT a double. So to get rid if try, catch blocks and some strnage errors, you can use TryParse method in this manner:

double answer;
            if (sender is System.Windows.Forms.Button)
            {
                if (double.TryParse(answerBox.Text, out answer))
                    answer = Convert.ToDouble(answerBox.Text);
                else
                    MessageBox.Show("inserted value \"" + answer + "\" is not a number (type double)!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

Because "answer" is only assigned a value under a condition.

If that condition is not met, you still try to use its value later.

You can avoid it by initially assigning it a value to begin with.

This line

setPicsToVisible();//this method sets pictures to non visible

gets the "Obscure Or Misnamed Method Of the Week" award.

setPicsToVisible();//this method sets pictures to non visible

I'm sorry, but that is hilarious to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.