0

I've been having trouble on understanding as of why my custom empty string validation method does not work compared when I check for an empty string directly

Validation.EmptyValidation(title,
"Please, do not leave the course title field empty!" +
"\r\nEnter the course title: ");

It does not output the course title in the end, but when I do it this way it does:

while (string.IsNullOrEmpty(title))
{
   Console.WriteLine("No empty string: ");
   title = Console.ReadLine();
}

Class:

 Console.WriteLine("* Create Course *\r\n");

 Console.WriteLine("Enter the course title: ");
 string title = Console.ReadLine();

 while (string.IsNullOrEmpty(title))
 {
    Console.WriteLine("No empty string: ");
    title = Console.ReadLine();
 }

 Validation.EmptyValidation(title,
 "Please, do not leave the course title field empty!" +
 "\r\nEnter the course title: ");

 Console.WriteLine("\r\nEnter the course description: ");
 string description = Console.ReadLine();
 Validation.EmptyValidation(description,
 "Please, do not leave the course description field empty!" +
 "\r\nEnter the course description: ");

 Console.WriteLine("\r\nEnter the number of students in the course: ");
 =string studentsInput = Console.ReadLine();
 int.TryParse(studentsInput, out int students);

 CreateCourse(currentCourse, title, description, students);

 public static Course CreateCourse (Course _currentCourse, string title string description, int students)
    {
        Course course = new Course(title, description, students);
        _currentCourse = course;
        _currentCourse.Title = course.Title;

        Console.WriteLine($"\r\nThank you for registering the {_currentCourse.Title} course.\r\n" +
          $"\r\nCourse Information" +
          $"\r\nTitle: {_currentCourse.Title}" +
          $"\r\nDescription: {_currentCourse.Description}" +
          $"\r\nStudents: {_currentCourse.Capacity}");

        return _currentCourse;
    }

Empty Validation Method:

    public static string EmptyValidation(string input, string prompt)
    {
        while (string.IsNullOrEmpty(input))
        {
            Console.WriteLine(prompt);
            input = Console.ReadLine();
        }
        return input;
    }
5
  • "why my custom empty string validation method does not work"... How could we possibly know if we cant see the code for Validation.EmptyValidation ? Commented Sep 27, 2018 at 6:27
  • Eh... where is the implementation for the Validation? Commented Sep 27, 2018 at 6:27
  • I forgot to add it, my bad. It's the last piece of code. Commented Sep 27, 2018 at 6:29
  • 2
    You start the Validation.EmptyValidation, but you ignore the return value. Commented Sep 27, 2018 at 6:33
  • 1
    I would suggest to use string.IsNullOrWhiteSpace otherwise the user can input " " (several spaces or tabs) and this would be valid in the current situation Commented Sep 27, 2018 at 6:43

1 Answer 1

2

There is a couple of things going wrong here

// you weren't returning the results 
title = Validation.EmptyValidation(title,
"Please, do not leave the course title field empty!" +
"\r\nEnter the course title: ");

Also if you don't need the other validation anymore you are best to remove it

//while (string.IsNullOrEmpty(title))
//{
//    Console.WriteLine("No empty string: ");
//    title = Console.ReadLine();
// }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.