title: Validating DateTime Input from Console in C#
body: <p>In a bid to learn C# in a hands-on manner, I started working through these assignments I found online...</p>
  
  <p><a href="http://www1.cs.columbia.edu/~lok/csharp/assignments.html" rel="nofollow">http://www1.cs.columbia.edu/~lok/csharp/assignments.html</a></p>
  
  <p>I am stuck on Exercise 1, Question 3.  How can I request a date from the console and then check against given criteria?  In this case, check if the date is after today and provide an 'invalid' message if true.</p>
  
  <p>My code currently looks like the below.  I feel like the logic is there, but something to do with scope won't let it compile - the userBirthdate seems inaccessible outside of the loop.  </p>
  
  <p>I'm sure there's also a much simpler solution.</p>
  
  <p>Any help greatly appreciated - thanks guys!</p>
  
  <pre><code>        //Set today's date as a variable            
          DateTime todayDate = DateTime.Today;
          DateTime userBirthdate;
  
          //Ask user for birth date
          Console.Write("Please enter your date of birth (dd/mm/yy):  ");
  
          //Validate the input and set as a variable
          bool inputValid = false;
          while (inputValid != true)
          {
              if (DateTime.Parse(Console.ReadLine()) &gt; todayDate)
              {
                  Console.Write("Invalid Date.  Please enter your date of birth (dd/mm/yy):  ");
              }
              else
              {
                  userBirthdate = DateTime.Parse(Console.ReadLine());
                  Console.WriteLine(userBirthdate);
                  inputValid = true;
              }
          }
  
          //Calculate user age
          int userAge = todayDate.Year - userBirthdate.Year;
          if (userBirthdate &gt; todayDate.AddYears(-userAge)) userAge--;
  
          //Output
          Console.WriteLine("You are {0} years old!", userAge);
          Console.ReadLine();
  </code></pre>