In this article we will see displaying of data with arguments, variables, if condition, for loop condition and try/catch statement.
Below are the links for other articles of Learn C# Step by Step :-
Learn C# Step By Step :- Part 1.
Learn C# Step By Step (Concept of Class and Object): - Part 3
Learn C# Part 4: Synchronous and Asynchronous methodology.
Learn C# Part 4b: Delegates, Multicast Delegates and Events.
Learn C# Part 5: Mini project - File Search.
Learn C# Part 6: Delegates - Lambda Expression, Expression Tree, Func<>, Action<> and Predicate<>
In the previous article we had seen how to do the installation of Visual Studio Community Edition and simple application in which we displayed a simple hard coded text on the console prompt of the console application.
Display data with Arguments
In this article, we will look into some more ways to displaying data. In this exercise will see displaying of hard coded data along with the arguments passed.
So below is the sample with passing arguments args & args with our hard coded line within our Console.WriteLine
Please Note: Argument will start with 0 followed by 1, 2, 3 and so on…C# has defined this way of representation for the argument. This way of presentation is termed as zero indexing.
Now we will see the output on the command prompt how it will be displayed. For that first Rebuild the solution by clicking Build > Rebuilt Solution on the menu of the Visual Studio.
Now in this exercise will use other way to run the application, first go to Debug folder where the application exe is present, next go to command prompt by typing “cmd” in the run command window. In order to open run command window just press window icon with ‘R’ key on the keyboard.
Once the command prompt window is open type “cd” followed by space and then paste path “C:\Users\Khadak\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug”.
It will enter into debug folder where the application is present. Now type the application “ConsoleApplication1” and argument 1 “theory” then argument 2 “practical”. Once enter is press on the keyboard exe will execute followed by argument passed and it will display the output
“Learning C# Step by Step theory practical”
As shown down below image output displayed in command prompt.
Displaying data using Variables
In actual if you see user do not enter values through arguments. Data is entered via keyboard and gets stored in variables within application. Variables are small memory location to store the data.
So next we will introduce variable in our program which gets input from the user and display name and age with hard coded text “Hello” in it. Follow below steps for the same on the source code: -
1) Declaring variable Name, Age with data type as string and its values as null.
2) Type a Console.WriteLine on console prompt to enter name followed by enter age.
3) Once it prompt user to enter Name and Age there should be a line which should take input from the user via keyboard and insert into console application, so for that write below code on the source code of program.cs
Name = Console.ReadLine();
Age = Console.ReadLine();
Then it will take input from user.
4) Next write a code with ‘+’ which will concatenate all hard coded text “Hello” followed by Name and Age and display the complete output.
5) Finally rebuilt the code before running exe file which is newly written for displaying output with variable.
In this exercise we will adopt faster means to run exe file on the console prompt and display the data instead of going to debug folder and then double clicking and executing the exe file.
After the solution is built we will just press “ctrl+ F5” on the keyboard in order to execute the exe file of the console application.
It will first prompt to enter name after entering the name “Ronnie” it will then prompt to enter age and after entering age “20” press enter it will show the final output displayed with hard coded text concatenated with variable. Here is how output which will look alike.
Good now output is getting displayed with variables as entered by the user. But still if you look at the “Age” field it is still accepting non-numeric characters which is words (twenty) because data type while declaring “Age” field is string as shown in the below output screen.
In such case “Age” field should accept only numeric and not alphanumeric for that we have change data type from “string” to “int” which accepts only numeric value.
Inserting Correct Data
In order to make changes so that correct numeric data should enter into “Age” field. So below are the some changes which is required to be made in to our source code.
1) Change data type of “Age” field from “string” to “int”
2) Change “Age” field value from “”(null) to 0 as shown in the image down below.
3) Write a code which converts the input inserted by user via keyboard which is taken as string in C# to integer. If this conversion is not done then it shows red under line error suggesting for conversion.
Now it is time to execute the application and check by pressing ctrl+F5 on the keyboard and check output. This time will enter alphanumeric characters and test whether it runs successfully or shows some error.
After executing the exe file and putting input as alphanumeric characters in “Age” field and pressing enter it will throw an error and application will crash.
So application is following in accordance as we wanted and prohibits to enter incorrect data in the “Age” field.
In order to avoid application crash in case of incorrect field entered we have to put proper exception handling to handle such situation.
Handling exception in the program
Next we will see how to handle exception by introducing try/catch statement in our existing source code.
Following are the changes which are need to be done in our source code:
1) Write try statement followed by open curly braces and move existing working code here and then put the close curly brace.
2) Next is to write the catch statement for the exception which is going to be occurred in case when incorrect data is entered by the user. So the catch statement will catch exception if any present to avoid crashing of application. It will give provision to add a custom error message in the form of display statement if any exception occurs so that user can rectify input errors.
First test application using correct field values and after successful execution now will run (.exe) by providing input by entering Name and Age with alphanumeric values and then press enter.
It will show the exception message “Incorrect DATA...Insert Correct Values” which is due to incorrect input given in the Age field. Instead of giving numeric value we have entered value which were alphanumeric.
So good our exception is getting correctly handled and avoided the application from crashing.
Implementing If Statement
While working in an application there are times where there is a need to check length of the characters entered in Name field like it cannot be more than 8 to 9 characters or Name field cannot remain empty.
And secondly for Age field which cannot be zero or cannot be more than 100. This can be termed as condition to ensure correct data in terms of length and proper data can be entered.
Condition 1: So we will take first case where we have “Name” field which cannot be empty i.e. length cannot be zero. If it happens where user press enter without entering value it will show a conditional error which is “Name cannot be empty”. For that write IF condition with statement (length==0) where length cannot remain zero. And under open curly write error message which will be shown when conditional error occurs for length if found 0. Then following line is for displaying the error message on the console prompt screen. As application has landed in conditional error there is also a “return” statement written which will exit the application and program will not move further.
Condition 2: Similarly same line of IF condition will be written for the “Age” field where condition is set that Age cannot be greater than 100. If it happens then error message would be displayed “Age not proper”.
Below is the snapshot of the output in both Condition 1 and Condition 2. Error message is being displayed if Name field is kept empty in Condition 1 and again if pressed enter key on the keyboard application will exit.
And in Condition 2 if the Age field is set more than 100 it will also show conditional error message where it cannot be set beyond set limit. Hence we were able practically to tackle two of the conditions for Name and Age field.
Implementing For Loop
There are scenarios when we have to enter multiple records in one go at that time implementing “For Loop” will come into the picture. Till now what use to happen we can make an entry of single record in one go i.e. only one record we can enter on console prompt and then the program used to exit and then again to enter new fresh record there is again need to repeat same process.
So by the Implementation of “For Loop” statement we can loop the entry of the records as many times we want. Just move our entire program into the curly braces of “For loop” and make our program better to achieve of multiple records in one go.
After the program is executed using ctrl+F5 it will open the command prompt screen and now under it you will find very first line is of “For loop” to enter the number of records we want to make its entry with text “Total number of records for entry”.
For explanation we have used 2 records for making entry. After first record entry is completed then it will display its result and now because of the “For loop” then will take input for the second record entry.
Hope you have learned and understood some more in practical’s from the real life problems and implemented changes using basic concepts of the programming. Till now we have seen programming which was of more kind of functional.
In the next lab we will make use of classes and objects which is actually used and culture of software industry.
As a fresher we would recommend you to go through learning with following fresher video from our Learn C# is 100 hrs series: -