Check Empty Input Using Recursion In Console Application

Introduction

 
In this article you are going to learn about how to take input and check if the entered value is empty or not using recursion in Windows Forms.
 
Tools
 
Visual Studio
 
Targeted Audience
 
People with basic knowledge of C# and Windows Forms.
 

Steps

 
First of all open Visual Studio and create a new project.
 
Check Empty Input Using Recursion In Console Application 
 
Select console application .NET framework from the main, name your project as you want and click OK.
 
Check Empty Input Using Recursion In Console Application 
 
We created our project successfully. It might take some time. Now open Program.cs file. It contains the code of your application and Main() function will be called automatically when you debug your application.
 
We have to write our Check Empty Value function. The simplest way to do this is using recursion in your method. In this we will take a parameter which will be the text that we will show to our user. After that we will read the text that will be entered by the user and check if the entered value is empty or not. If it is empty we will show an error message to the user and ask him to enter a value again by calling the method itself again, if it is not empty we will let him go through.
 
Above your Main() function create a global variable “EnteredVal” with the string data type. After that just paste the code below your Main() function.
 
C# Code
  1. static void CheckEmptyVal(string EnterText)  
  2.         {  
  3.             try  
  4.             {  
  5.                 Console.Write(EnterText);  
  6.   
  7.                 string enteredVal = Console.ReadLine();  
  8.   
  9.                 if (!string.IsNullOrWhiteSpace(enteredVal))  
  10.                 {  
  11.                     EnteredVal = enteredVal;  
  12.                     return;  
  13.                 }  
  14.                 else  
  15.                 {  
  16.                     Console.WriteLine("Empty value not allowed.");  
  17.                     CheckEmptyVal(EnterText);  
  18.                 }  
  19.             }  
  20.             catch (Exception ex)  
  21.             {  
  22.                 throw ex;  
  23.             }  
  24.         }  
This function will check the entered value and ask the user to enter again if he enters an empty value. Now in your Main() function create an “enterText” string variable and assign any input text you want to show the user. Call your function and pass the enterText as a parameter. In the last just use Console.ReadKey() to see the output.
 
All your code will look something like given below.
 
C# Code
  1. using System;  
  2.   
  3. namespace ProjCheckEmpValConsole  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static string EnteredVal = "";  
  8.         static void Main(string[] args)  
  9.         {  
  10.             string enterText = "Enter Your Name.";  
  11.             CheckEmptyVal(enterText);  
  12.             Console.ReadKey();  
  13.         }  
  14.   
  15.         static void CheckEmptyVal(string EnterText)  
  16.         {  
  17.             try  
  18.             {  
  19.                 Console.Write(EnterText);  
  20.   
  21.                 string enteredVal = Console.ReadLine();  
  22.   
  23.                 if (!string.IsNullOrWhiteSpace(enteredVal))  
  24.                 {  
  25.                     EnteredVal = enteredVal;  
  26.                     return;  
  27.                 }  
  28.                 else  
  29.                 {  
  30.                     Console.WriteLine("Empty value not allowed.");  
  31.                     CheckEmptyVal(EnterText);  
  32.                 }  
  33.             }  
  34.             catch (Exception ex)  
  35.             {  
  36.                 throw ex;  
  37.             }  
  38.         }  
  39.   
  40.     }  
  41. }  
Now let’s run the code and see the output.
 
Check Empty Input Using Recursion In Console Application 
 
You can see the text you write in the code. Now just don’t write anything and press enter. It will show an error message in the console.
 
Check Empty Input Using Recursion In Console Application 
 
Now try this with any value like your name etc. it will not show any error and let you go through.
 

Conclusion

 
This article teaches the use of recursive method in Console application and also asks the user for a not-empty value every time he enters an empty one.


Recommended Free Ebook
Similar Articles