title: How to create an array storing PictureBox names in C#
body: <p>I want to create an array of picturebox names, which I will later use in a FOREACH loop and do a certain thing to each picturebox instead of having to write the code for all of the pictureboxes separately. In another solution the code below worked, but here it only returns an error: "A field initializer cannot reference the non-static field, method, or property 'Puzzle.Form1.pic1'"</p>
  
  <p>I could not find what I was looking for on the internet, even though I tried to write it in different ways. Thanks!</p>
  
  <pre><code>    //Define an array and place the 9 picture boxes in it
  
      //System.Drawing. [] PictureBox = {}
      //PictureBox[] Pictures1 = new PictureBox[9];
      //PictureBox[] Pictures1 = { pic1, pic2 };
      //PictureBox pBoxes = new PictureBox[] {pic1, pic2, pic3};
  
      PictureBox[] diceloc = { pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9 };
  </code></pre>