Testing for the existence of an integer in C#

I’m working on a method to request an input from the console, and keep requesting it until one is provided - if the user just hits return rather than typing an integer, it just keeps asking again.

The relevant part looks as follows:

      string redThreshold;
      int missedLecturePercentageThresholdRed;

      // Request threshold value for 'red' RAG status of lectures attended
      do
      {
        Console.Write("Enter the threshold percentage of lectures missed for a red RAG Status: ");
        redThreshold = Console.ReadLine();

        if (!int.TryParse(redThreshold, out missedLecturePercentageThresholdRed))
          continue;
      }
      while (string.IsNullOrEmpty(missedLecturePercentageThresholdRed.ToString()));

      Console.WriteLine("Missed lecture percentage is: " + missedLecturePercentageThresholdRed.ToString());

Currently, if I hit return the output is as follows:

Enter the threshold percentage of lectures missed for a red RAG Status: 
Missed lecture percentage is: 0
Enter the threshold percentage of lectures missed for an amber RAG Status: 

Somewhere in there, rather than being null or empty as I’d expected, it is represented as 0 and so passes the while condition and skips to the next do while for amber.

How would I go about modifying things to get the expected result?

Is there a way to force “binary safe” comparison?

For the moment, I’ve taken a slightly different tack…

      string redThreshold;
      int missedLecturePercentageThresholdRed = -1;

      // Request threshold value for 'red' RAG status of lectures attended

      if (missedLecturePercentageThresholdRed < 0)
      {
        do
        {
          Console.Write("Enter the threshold percentage of lectures missed for a red RAG Status, or hit return to accept default value (30): ");
          redThreshold = Console.ReadLine();

          if (!int.TryParse(redThreshold, out missedLecturePercentageThresholdRed))
            missedLecturePercentageThresholdRed = 30;
          Console.WriteLine("Red RAG status threshold set to: " + missedLecturePercentageThresholdRed);
        }
        while (missedLecturePercentageThresholdRed == -1);
      }

Which gives…

Enter the threshold percentage of lectures missed for a red RAG Status: 
Red RAG status threshold set to: 30
Enter the threshold percentage of lectures missed for an amber RAG Status: 

In theory, it should never hit the while condition, as missedLecturePercentageThresholdRed should always get set to something in the do block, but if in event it does remain at -1, then it’ll kick it back through the do again.

Only a guess, but should there be a “this.variable” to explicitly reference the one assigned the value of the signed int “-1” ?

:man_shrugging:

The solution is less complicated, not more complicated. You are testing for a valid integer in an indirect manner. You convert the string to an integer then convert that string back to an integer. That is so very unnecessary. Your do while condition can be and should be the result of the TryParse. The result will be false if the string is empty and you want to continue in that situation. Just do while TryParse is false (actually !TryParse).

1 Like

I agree with @SamuelCalifornia

You’ll want something like this:

      string redThreshold;
      int missedLecturePercentageThresholdRed;

      // Request threshold value for 'red' RAG status of lectures attended
      do
      {
        Console.Write("Enter the threshold percentage of lectures missed for a red RAG Status: ");
        redThreshold = Console.ReadLine();
      }
      while (!int.TryParse(redThreshold, out missedLecturePercentageThresholdRed));

      Console.WriteLine("Missed lecture percentage is: " + missedLecturePercentageThresholdRed.ToString());
2 Likes

I meant to say then convert that integer back to a string.