Search code examples
c#visual-studio-2017

(WindowsFormsApp) How to make my code remember a variable?


I'm making a program that checks numbers to see if they're prime numbers in Visual Studio 2017 as a windows forms app. I've got this so far, without any errors:

            int i;
            string i2;
            int a;
            int j;
            i = 0;
        CNN:
            i = i + 1;
            a = 2;
            for (j = 2; j < i; j = j + 1)
                if (i % j == 0)
                    a = a + 1;
            {
                if (a == 2)
                    i2 = Convert.ToString(i);
                else
                    goto CNN;
            }
            label1.Text = i2 + ", ";

This code is inside a button, and is supposed to send i2 (the prime number it's found) to a label. How do I get the code to remember what i2 was, and rerun the code with i = i2 instead of i = 0 every time I click the button? (While starting off at i = 0 when I first run the application)


Solution

  • Remember it in i2.

    i2 += (i2 == null ? "" : ", ") + Convert.ToString(i);
    

    And in the end just

    label1.Text = i2;