title: Overloading a method in C#
body: <p>I'm working on an assignment and I am stuck on the overload my method to repeat it <code>nth</code> times thats specified in the <code>main</code>. I have the last method in my class called <code>Collatz</code> wrapped in a <code>do while</code> loop but it doesn't seem to be working correctly as it should run the loop x amount of times as specified in the main method where it is being overloaded. It's the only part I can't figure out. Heres is my <code>Main</code>:</p>
  
  <pre><code>static void Main(string[] args)
          {
              Collatz thisCollatz = new Collatz(15);
              Console.WriteLine(
                  string.Format("Collatz iteration is {0} and value is {1}",
                  thisCollatz.Iteration, thisCollatz.CurrentVal));
  
              thisCollatz.Iterate();
              Console.WriteLine(
                  string.Format("Collatz iteration is {0} and value is {1}",
                  thisCollatz.Iteration, thisCollatz.CurrentVal));
  
              //// Overload of iterate method
              thisCollatz.Iterate(10);
              Console.WriteLine(
                  string.Format("Collatz iteration is {0} and value is {1}",
                  thisCollatz.Iteration, thisCollatz.CurrentVal));
  
              Console.WriteLine("\nPAK..." + "\nProduced By: Jeremy Garcia");
              Console.ReadKey();
          }
  </code></pre>
  
  <p>and here is what I have in my class:</p>
  
  <pre><code>class Collatz
      {
          // _Iteration is how many iterations of Collatz have run since initialization
          // _CurrentVal is the current value of the Collatz process
          private int _Iteration;
          private int _CurrentVal;
  
          public Collatz(int InitialValue)
          // initializer
          {
              CurrentVal = InitialValue;
              Iteration = 0;
          }
  
          public int CurrentVal
          // returns the current Collatz value or -- within the class -- allows it to be set
          {
              get { return _CurrentVal; }
              private set
              {
                  if (value &gt; 0)
                  {
                      _CurrentVal = value;
                  }
                  else
                  {
                      throw new Exception("CurrentVal is not a positive integer");
                  }
              }
          }
          public int Iteration
          // returns the current number of Collatz iterations or 
          //  -- within the class -- allows it to be set
          {
              get { return _Iteration; }
              private set { _Iteration = value; }
          }
  
          public void Iterate()
          // Executes one iteration of Collatz creating a 
          // new CurrentVal from the existing CurrentVal
          {
              if (_CurrentVal != 1)
              {
                  if (_CurrentVal % 2 == 0)
                  {
                      _CurrentVal = _CurrentVal / 2;
                      _Iteration ++;
                  }
                  else if (_CurrentVal % 2 != 0)
                  {
                      _CurrentVal = (_CurrentVal * 3) + 1;
                      _Iteration ++;
                  }
                  else
                  {
                      _CurrentVal = 1;
                      _Iteration = 1;
                  }
              }
          }
          public void Iterate(int iterations)
          // Check if CurrentVal is (already) 1 -- don't calculate 
          //  new CurrentVal nor increment Iteration if so
          // Otherwise calculate new CurrentVal and increment Iteration
          {
              do
              {
                  if (_CurrentVal != 1)
                  {
                      if (_CurrentVal % 2 == 0)
                      {
                          _CurrentVal = _CurrentVal / 2;
                          _Iteration++;
                      }
                      else if (_CurrentVal % 2 != 0)
                      {
                          _CurrentVal = (_CurrentVal * 3) + 1;
                          _Iteration++;
                      }
                      else
                      {
                          _CurrentVal = 1;
                          _Iteration = 1;
                      }
                  }
              } while (iterations != 0);
          }
      }
  </code></pre>
  
  <p>Thank you for the help in advance. I'm not looking to rewrite the entire program, just simply get the last method to work. Thanks again.</p>