-1

In the current page (which is contained inside a frame inside MainWindow) I have this happen when I click a button:

    for (int i = 0; i < mw.pizzas.Length + 1; i++)
    {
        if (i == 10)
        {
            MessageBoxResult result = MessageBox.Show("You can't order more than 10 pizzas");
            break;
        }
        if (mw.pizzas[i] == null)
        {
            mw.pizzas[i] = pizza;
            break;
        }
    }
    NavigationService.Content = new AnotherPage();

The mw.pizzas string array contains 10 (I think undefined) variables. I have defined it in MainWindow:

public string[] pizzas = new string[10];

I have defined it on the current page, like so:

MainWindow mw = new MainWindow();

This loop increments by defining these 10 slots with the contents of the variable "pizza". When all of the indexes of the array are defined, a message should pop up. However, this message never shows up. What am I doing wrong here?

Edit: This is just a wild guess. It might have to do with me creating a new CurrentPage every time this page is navigated to. Every time one of these indexes is defined, this navigates back to the previous page, then uses NavigationService.Content = new CurrentPage();

Edit 2: Looks like there is something wrong with the second statement. mw.pizzas[0] is null, then gets the contents of pizza in it, then somehow goes back to being null after the program exits the page and enters it once again.

Edit 3: I tried to make the pizzaIDs array static, but I get the following error: Member 'MainWindow.pizzaIDs' cannot be accessed with an instance reference; qualify it with a type name instead

Seminix
  • 97
  • 9
  • 1
    That is because you keep `new`ing up new `MainWindow`s. Each `MainWindow` has its own copy of `pizzas`. You need to either change `pizzas` to be `static` or only `new` up one `MainWindow`. – mjwills Mar 18 '18 at 23:30
  • I changed it to be static but got this error: Member 'MainWindow.pizzas' cannot be accessed with an instance reference; qualify it with a type name instead – Seminix Mar 18 '18 at 23:36
  • 1
    That exception is self explanatory. Use `MainWindow.pizzas` instead of `mw.pizzas. – mjwills Mar 18 '18 at 23:47

1 Answers1

0

You loop goes from 0 to 9 inclusive, the array indicies, when it hits 10, it breaks out of the loop and doesn't execute.

Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
  • I added + 1 to the length `i < mw.pizzas.Length + 1`. Even now, after I changed it to 9, it still doesn't work. – Seminix Mar 18 '18 at 22:56
  • Ah, missed that. Sorry about that. Have you tried putting a breakpoint on the messagebox line? – Joel Lucsy Mar 18 '18 at 22:59
  • The messagebox line is also up there in the code. I believe I did. – Seminix Mar 18 '18 at 23:00
  • I see that. The question is, did you actually hit the line and it simply didn't show? I've had that happen when executed on a non-gui thread and other times. – Joel Lucsy Mar 18 '18 at 23:02
  • I think the problem occurs because the condition doesn't ever get satisfied. I did test this code outside of the condition, and the message box did show up. – Seminix Mar 18 '18 at 23:08