I have been looking at tutorials in regards to isNullOrWhiteSpace to validate user input of a string.

but i am unsure how to initialise the method. This is just a code snippet and all i am trying to do is place this in a loop to ensure if user presses enter it cannot skip to the next entry. Can anyone point me in the direction for some good info for someone new to c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PracticalTest5
{
    class Program
    {
        public static void Main(string[] args)
        {
            {
               string movie_title = "";
{
Console.Write("Movie Title : ");
movie_title = Convert.ToString(Console.ReadLine());
}

Recommended Answers

All 3 Replies

Line 17: Console.ReadLine already returns a string so Convert.ToString is not needed.
You could use the string property Lenght to test if a string is empty.

IsNullOrWhiteSpace is a static method so you use it by class.method, i.e.

if (String.IsNullOrWhiteSpace(movie_title)) {
    Console.WriteLine("You must enter a movie title, noob!");
} else {
    // they entered a title, do whatever you want here
}

Thanks Momerath.

All fixed

Console.Write("Movie Title : ");
                                        movie_title = (Console.ReadLine());

                                        while (string.IsNullOrWhiteSpace(movie_title))
                                        {
                                            Console.ForegroundColor = ConsoleColor.Red;
                                            Console.WriteLine("You must enter a movie title.");
                                            Console.ResetColor();
                                            Console.Write("Movie Title : ");
                                            movie_title = (Console.ReadLine());
                                        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.