Updating my mind

When you're learning something new and eventually you figure it out, you got a fresh algorithm or rules in your mind, to understand how to use that. At that point you can explain it to somebody else and he/she will most likely understand the thing faster. However, when you become an expert your brain works automaticly and it is more difficult for you to explain it to somebody else. You forget that it is not straigforward to figure that out when you don't know it yet. Somebody else might be yourself within months, after a while without using that thing. This is a another good reason to write documentation and unit tests as the functionality is being implemented, that is, as the code is being written.

I had a bunch of ideas on how to explain the rationale behind applying generics when I was learning them. Now I just use strongly type structures and generic collections as much as I can but I would find it difficult to explain.

I'm know trying to leverage "new" features of C# and this time I'll post my thoughts about them. One useful manner of taking advantage of Anonymous Delegates is to use them as event handlers.

   1:          private void createDynamicPlaceholders()
   2:          {
   3:              StackPanel panel = new StackPanel();
   4:              panel.HorizontalAlignment = HorizontalAlignment.Left;
   5:              panel.Orientation = Orientation.Horizontal;
   6:  
   7:              Label pickLabel = new Label();
   8:              pickLabel.Content = "Pick ";
   9:              Label ofLabel = new Label();
  10:              ofLabel.Content = " of ";
  11:  
  12:                  // LOOK THIS:
  13:              IntegerTextBox cumulativeBox = new IntegerTextBox();
  14:              IntegerTextBox maxPicksBox = new IntegerTextBox();
  15:              maxPicksBox.KeyUp +=
  16:                  delegate(Object sender, KeyEventArgs args)
  17:                  {
  18:                      cumulativeBox.MaxValue = maxPicksBox.Integer;
  19:                      if (cumulativeBox.Integer > maxPicksBox.Integer)
  20:                      {
  21:                          cumulativeBox.Integer = maxPicksBox.Integer;
  22:                      }
  23:                  };
  24:  
  25:              panel.Children.Add(pickLabel);
  26:              panel.Children.Add(cumulativeBox);
  27:              panel.Children.Add(ofLabel);
  28:              panel.Children.Add(maxPicksBox);
  29:  
  30:              _dynamicPanel.Children.Add(panel);
  31:          }

If there wasn't an anonymous delegate I'd need a subscriber method but more importantly, I'd need to save the reference to the two textboxes, as the code in the subscriber needs them. It would imply having some kind of structure in memory to reference them, it means more lines of ugly code.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://www.ciekawe.listy.elk.pl/ Eufemiusz

    What an fascinating write-up, keep on crafting spouse

  • http://www.katalog.znajomi.bieszczady.pl/ Jeremiasz

    Partner, this amazing site is fabolous, freezing think itrrrs great

  • http://www.lista.znajomi.bieszczady.pl/ Gos³aw

    hey, your site is really unquie. Anways, i do thank you for deliver the results