title: How to implement re-usable code with multiple UI elements in .Net Framework?
body: <p>I have a struct full of data members that will be used to populate some textboxes and all of the text boxes will have the same functionality. For instance, the background color might change based on the range of the value inside the textbox (the value comes from a data member from the struct), or the struct data member might be updated if you change the content on the textbox and press enter.</p>
  
  <p>I want my implementation to take into account that I may add more textboxes linked to other data members from the struct. I was thinking of using an array of tuples of type  for instance that would relate each pair of TextBox and member from the struct. The problem is that ints aren't treated as references in C# and pointers are not a good idea, so I can't just put each data member from the struct in this array.</p>
  
  <p>If I create a struct like this</p>
  
  <pre><code>struct MyValues
  {
      int val1;
      int val2;
      int val3;
  }
  
  MyValues values = new MyValues;
  </code></pre>
  
  <p>I want to be able to declare an array of tuples</p>
  
  <pre><code>private static Tuple&lt;TextBox, int&gt;[] tuples =
  {
      (textBox1, values.val1),
      (textBox2, value.val2),
      (textBox3, values.val3),
  };
  </code></pre>
  
  <p>And have functions that use arrays of this type for doing operations on the TextBoxes using the struct's data members.</p>
  
  <pre><code>private void Update(Tuple&lt;TextBox, int&gt;[] tuples)
  {
      foreach(Tuple&lt;TextBox, int&gt; t in tuples)
          t.Item1.Text = t.Item2.ToString();
  }
  </code></pre>
  
  <p>This can't be done like this because the int members of the struct are copied by value in the tuples array initialization (and not by reference). I was thinking of wrapping the int in a new object type so I could copy by reference, but maybe there is a better solution.</p>
  
  <p>How can I make this work? Should I encapsulate int in an object? Is there a better design pattern for this kind of situation?</p>
  
  <p>Thanks</p>