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.
Select console application .NET framework from the main, name your project as you want and click OK.
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
- static void CheckEmptyVal(string EnterText)
- {
- try
- {
- Console.Write(EnterText);
-
- string enteredVal = Console.ReadLine();
-
- if (!string.IsNullOrWhiteSpace(enteredVal))
- {
- EnteredVal = enteredVal;
- return;
- }
- else
- {
- Console.WriteLine("Empty value not allowed.");
- CheckEmptyVal(EnterText);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
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
- using System;
-
- namespace ProjCheckEmpValConsole
- {
- class Program
- {
- public static string EnteredVal = "";
- static void Main(string[] args)
- {
- string enterText = "Enter Your Name.";
- CheckEmptyVal(enterText);
- Console.ReadKey();
- }
-
- static void CheckEmptyVal(string EnterText)
- {
- try
- {
- Console.Write(EnterText);
-
- string enteredVal = Console.ReadLine();
-
- if (!string.IsNullOrWhiteSpace(enteredVal))
- {
- EnteredVal = enteredVal;
- return;
- }
- else
- {
- Console.WriteLine("Empty value not allowed.");
- CheckEmptyVal(EnterText);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
-
- }
- }
Now let’s run the code and see the output.
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.
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.