0

I am creating a windows Form app In C#.
It is for a course I follow.

Now I have a

Cannot evaluate expression because a native frame is on the top of the call stack.

error. I have 10 buttons, with a for loop connected to it. The app has 10 buttons and each button show math tables (1 to 10) to Label2. The first button works like a charm. The other buttons give me the expression error.

Here I have the code for the first 2 buttons:

private void button1_Click(object sender, EventArgs e)
    {
        // Clear Label before execution
        label2.Text = "";

        // Loop
        for (int n = 0; n < 11; n++)
        {
            int nn = n * 1;
            label2.Text += "1x" + n + "=" + nn + "\r\n";                
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
       // Clear Label before execution
        label2.Text = "";

        // Loop
        for (int n = 0; n < 11; n = n++)
        {
            int nn = n * 2;
            label2.Text = "2x" + n + "=" + nn + "\r\n";                                             
        }            

    }

Can you guys help me out on this one? I already read through thread handling and such, but I am not that far yet into C#.

Edit: Got it to work with a change in the loop,

From

for (int n = 0; n < 11; n = n++)

To

for (int n = 0; n < 11; n++)
zx485
  • 28,498
  • 28
  • 50
  • 59
  • 1
    Just a general pointer, but you should give your variables meaningful names. It is very unclear what `nn` is supposed to be, for example. – Broots Waymb Apr 20 '18 at 14:28
  • Also setting a break point, running the debugger and stepping through your code can do wonders. – Filburt Apr 20 '18 at 14:31
  • It is not clear what you did to get the debugger to break. Not likely with a breakpoint if a native frame is on the top of the stack, required. – Hans Passant Apr 20 '18 at 14:32
  • Probably using the "Break All" feature in Visual Studio. I use it frequently to find out where the infinite loop is. – Wazner Apr 20 '18 at 14:33
  • That can not be the only change to get it working. You also need to change the `label2.Text = .. ` to `label2.Text += ... ` to get it working – GuidoG Apr 20 '18 at 15:53

3 Answers3

2

Yepp... n = n++ is doing nothing.

It's explained here.

james dean
  • 71
  • 5
1

Change n = n++ to just n++ in your second loop.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
  • Does not work, I want the outputs of 2x1 till 2x10. With this fix I only get 2x10=20... – Nick de Vries Apr 20 '18 at 14:51
  • then post how you got it working for others with the same problem – GuidoG Apr 20 '18 at 15:16
  • This will not fix all of his problems. He also needs to change `label2.Text = ...`to `label2.Text += ... `I Tested it in my answer and then it works. Without this change he will only see the last update – GuidoG Apr 20 '18 at 15:55
0

First your loop should have n++ in stead of n = n++
Second you do not concatinate your label text.

it should be

label2.Text += ...

in stead of

label2.Text = ...
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • The Label2.Text +=, I know. The n++ option does not work. When I only have n++ it will only print out the last sum... (2x10) I want the button to print out every sum... – Nick de Vries Apr 20 '18 at 15:12
  • that is because you only wrote the last one in the label, the += should fix that – GuidoG Apr 20 '18 at 15:15
  • I have tested and with my adjustments it works like a charm – GuidoG Apr 20 '18 at 15:25