Monday, October 3, 2011

.NET interview questions: - How to check if data is of a proper data type?

This is not one of the typical .NET interview questions, but being as a .NET developer this can help you in some extent to check the data you have inserted as input is of proper data type or not.

Many of the developer friends use regular expression for this purpose but regular expression will be too much for simpler numeric validation, regular expression is good for complicated check like Email address check or some kind of complicated pattern of
data.

For simple data type where we just have to check whether the inputted data
isnumeric
or proper date validation for such kind of check we must
have something simpler. For the same we have something called as TryParse
function in every data type.

TryParse: - The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that is invalid and cannot be successfully parsed.

Now, let’s see a small and simple demonstration to understand the concept of using TryParse in much better way.

In order to see it practically just follow the following steps.

Step1: - Create a new Project of Console Application for that just go to >> File >> New >> Project >> Windows >> Select Console Application.





Step2: - Now, just add the below code snippet in the main class.

static void Main(string[] args)
{
Console.WriteLine("Please Enter a Data To check is of proper DataType");
string str = Console.ReadLine().ToString();
}

In the above code snippet you can see that I am taking some data from the Console.ReadLine() and I would like to check if the data entered is of “int (integer)” type or must be some type of numeric.

Now, let’s take a simple example to check whether the inputted data is numeric or not.


Now, just simply add the below code snippet to check whether the entered data is
of proper data type or not.




static void Main(string[] args)
{
Console.WriteLine("Please Enter a Data To check is of proper DataType");
string str = Console.ReadLine().ToString();
int output = 0;
bool isNumeric = int.TryParse(str, out output);
if (isNumeric == true)
{
Console.WriteLine("The Data You have entered is of proper DataType");
}
else
{
Console.WriteLine("The Data You have entered is not of proper DataType");
}
Console.ReadLine();
}

In the above code snippet you can see that I have simply created a bool variable and have defined it as int.TryParse which will actually take the data from the ((string str = Console.ReadLine().ToString();)) and if the TryParse function is successful to parse the data if it is numeric then the final result will be stored inside the output variable.

Let me just try to explain the above mentioned point.

If you have done pasting with the above code snippet now just simply run your
application to see the resultant output.

Before start executing your Console Application just place a small debug pointer
on the below mentioned diagram.





Now, when you run your application you will see the below output diagram.





Now, let put a wrong data to check whether the TryParse function is able to parse the data or it fails to do it.





In the above diagram you can clearly see that the isNumeric variable value is false because the TryParse function unable to parse the data and when you will see data of output variable you will find data like below diagram.





The below diagram is the output of the above mentioned data.





Now, let’s place a proper data and see the result of it.





In the above diagram you can clearly see that now the value of the isNumeric variable is true means the TryParse function has successfully parse the data. Let also the value of the output variable.





Now, you can see that the value of the output variable as 123.

The below is the result for the above entered data.


Similarly, you can test for the other data type.

See the following video on how to check if data is of a proper data type or not: -





See our more interview questions and answers for .NET

Regards,

View author’s other Most asked .NET interview questions



No comments: