Skip to main content

eBuisiness's answer is correct. But you're setting the HasBeenHandled flag after trying to update all the neighbors which then causes a stack overflow. You should instead set the flag before doing anything else.

EDIT: You've also made typos in the if statements.

  if (Bottom.btn != null)
      set_text_of_button(Bottom.btn);
  if (Bottom_Left.btn != null)
      set_text_of_button(Bottom_Left.btn);
  if (Bottom_Right.btn != null)
      set_text_of_button(Bottom_Right.btn);

should be

  if (Bottom != null)
      set_text_of_button(Bottom.btn);
  if (Bottom_Left != null)
      set_text_of_button(Bottom_Left.btn);
  if (Bottom_Right != null)
      set_text_of_button(Bottom_Right.btn);

Also, you'll need to stop this habit of initializing variables by allocating dummy objects. It is better to assign null than create a new object you're not going to use. For example, your find_by_coordinates function would be better written as

  protected Block find_by_coordinates(int x,int y)
  {
      Block return_value = null;
      foreach (Block blk in arr_buttons)
      {
          if (blk.x == x && blk.y == y)
          {
              return_value = blk;
              break;
          }
      }
      return return_value;
  }

Now, when the coordinate has no button a null value is returned instead of a dummy Block object which was leading to NullReferenceExceptions because the program was trying to work with these dummy objects.

Anton
  • 434
  • 4
  • 9