title: Head First C# exercise
body: <p>I came up with a different method while attempting to solve an exercise in the book "Head First C#" than the given solution.  I'm pretty new to C# and programming in general, and I want to understand if and why the book's method is better than mine.</p>
  
  <p>I'll detail the problem, then provide their answer and my answer.  Please give me as detailed and as simple/understandable answer as you can!</p>
  
  <blockquote>
    <p>Create a program with an Elephant class.  Make two Elephant instances
    and then swap the reference values that point to them, without getting
    any Elephant instances garbage-collected.  Make a form (Windows Forms
    Application) with 3 buttons.  The first button says "Lloyd", the
    second says "Lucinda", and the third says "Swap".  Clicking on the
    Lucinda button or the Lloyd button pops up a new window that displays
    the message "My ears are (EarSize) inches tall."  The title of the
    popup window is   "(Name) says...", with name being either Lucinda or
    Lloyd.</p>
    
    <p>You are supposed to create the Elephant class with an integer field
    called EarSize, and a string field called Name.  WhoAmI() is the
    method that displays the popup message box.  Create two elephant
    instances and a reference; the two Elephant fields are added to the
    Form1 class and are named Lloyd and Lucinda.  They're initialized;</p>
  
  <pre><code>lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 }; lloyd =
  new Elephant() { Name = "Lloyd", EarSize = 40 };
  </code></pre>
    
    <p>Make the Lloyd and Lucinda buttons work.  The Lloyd button calls
    <code>lloyd.WhoAmI()</code> and the Lucinda button calls <code>lucinda.WhoAmI()</code>.</p>
    
    <p>Hook up the swap button.  Make the swap button exchange the two
    references, so that when you click swap, the Lloyd and Lucinda
    variables swap objects and a "Objects swapped" box is displayed.  The
    first time you click Swap, if you click the Lucinda button the Lloyd
    box should come up, and if you click the Lloyd button the Lucinda box
    should come up.  If you click Swap again, when you click Lloyd or
    Lucinda their regular box should come up as normal.  If you click the
    swap button again, the objects switch again and... you get the idea.</p>
  </blockquote>
  
  <p>HERE is the code that the book provides as the correct answer:</p>
  
  <pre><code>using System.Windows.Forms;
  
  class Elephant 
  {
    public int EarSize;
    public string Name;
  
    public void WhoAmI()
    {
       MessageBox.Show("My ears are " + EarSize + " inches tall.", Name  + " says...");
    }
  }
  </code></pre>
  
  <p>****************************NEW CLASS*****************************</p>
  
  <pre><code>public partial class Form1 : Form 
  {
    Elephant lucinda;
    Elephant lloyd;
  
    public Form1()
    {
      InitializeComponent();
      lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 };
      lloyd = new Elephant() { Name = "Lloyd", EarSize = 40 };
    }
  
    private void button1_Click(object sender, EventArgs e)
    {
      lloyd.WhoAmI();
    }
  
    private void button2_Click(object sender, EventArgs e)
    {
      lucinda.WhoAmI();
    }
  
    private void button3_Click(object sender, EventArgs e)
    {
      Elephant holder;
      holder = lloyd;
      lloyd = lucinda;
      lucinda = holder;
      MessageBox.Show("Objects Swapped");
    }
  }
  </code></pre>
  
  <p>************************MY ANSWER BELOW*****************************</p>
  
  <p>The code shown so far was the two classes that makeup the books correct solution.  I will now paste the solution that I came up with below here.  My Elephant class is the same as the book's, but my Form1 class is different, so I will just post the class that is different.</p>
  
  <pre><code>public partial class Form1 : Form
  {
      Elephant lucinda;
      Elephant lloyd;
      public int counter = 0;
  
      public Form1()
      {
          InitializeComponent();
          lucinda = new Elephant() { Name = "Lucinda", EarSize = 33 };
          lloyd = new Elephant() { Name = "Lloyd", EarSize = 40 };
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          if (counter % 2 == 1)
          {
              lucinda.WhoAmI();
          }
          else
          {
              lloyd.WhoAmI();
          }
      }
  
      private void button2_Click(object sender, EventArgs e)
      {
          if (counter % 2 == 1)
          {
              lloyd.WhoAmI();
          }
          else
          {
              lucinda.WhoAmI();
          }
      }
  
      private void button3_Click(object sender, EventArgs e)
      {
          counter += 1;
          MessageBox.Show("Objects Swapped");           
      }
  }
  </code></pre>
  
  <p>Please, I would appreciate any insight into the differences between these methods,which is better, and why that's so.</p>
  
  <p>Thanks in advance guys!</p>