title: C# how to create while loop to validate if object is null
body: <p>I am having issues with two validations working together, one in the Main method and the other one in the Controller Class.</p>
  
  <p>So this is my static method inside the Main. Basically I ask the user to input the ID for an "Employee", the ID is a property of a Class named Employee, and then I show a message saying if that employee exists or not. Let's assume that I have several Employee objects already created. I know it's a form of while loop but I just cant get it right. </p>
  
  <p>My method inside the controller class is correct so there is no need to change anything. Basically it takes the ID and returns null if the Employee object does not exist or returns the object if it does exist. I need to take that null if the employee doesnt exist and create a loop that keeps asking the user to input a correct ID until he does, then the loop ends. Thank you, sorry if I did not explain it correctly.</p>
  
  <pre><code>    public static void mymethod()
      {
          Console.WriteLine("Please enter your Employee ID: ");
          int ID = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine("----------------------------");
          Employee employee = controllerclass.findemployee(ID);
          int i = 0;
          bool flag = false;
          while (!flag)
          {
              if (!flag)
              {
                  if (ID != null)
                  {
                      Console.WriteLine("The employee exists");
                      flag = true;
                  }
                  else
                  {
                      Console.WriteLine("Please enter a valid ID");
                  }
              }
              i++;
          }
  </code></pre>
  
  <p>My method inside the controller class is correct so there is no need to change anything. Basically it takes the ID and returns null if the Employee object does not exist or returns the object if it does exist. I need to take that null if the employee doesnt exist and create a loop that keeps asking the user to input a correct ID until he does, then the loop ends. Thank you, sorry if I did not explain it correctly</p>