Console Application – Chapter one
Objectives of the Chapter
· Starting the console application
· Learning basic functions
· Get started with basic programs
· Understand the coding in VS.net
Starting the console based program in Visual Studio.net
Follow the following steps to run the console based application.
1. Run Microsoft Visual Studio 2005 or other versions. (Start-programs-Microsoft Visual Studio2005-Microsoft Visual Studio 2005)
2. From file, select new project…
3. In the project type-select windows and chose console application in the template. Give the name of your project at the bottom. Choose ok.
Then you will see the basic structure of a VB.Net Console based Program.
Module Module1
Some basic programs and side by side understanding functions
Here is the most basic program…
Module Module1
Sub
System.Console.Write("console") 'This line traces the word console
System.Threading.Thread.Sleep(2000) 'This line stops the program for 2 seconds before ending
End Sub
End Module
To run the program press F5 key or click the green play button on the tool bar.
Description:-
By default your program will start with the object module1. (If you want, you can change the module name. i.e. module module_name. Then you will have to select the first object to be executed. For this, on the solution explorer panel, right click your project name and select properties. Then select the desired module name in the startup object.)
Sub main is the main function of your program. This main function must always appear in your program. Later you will learn to make other functions as desired. For now, just remember that main() function is the must. Sub is the keyword to define the function name which does not return any value.
The function write is used to trace desired information. This function is inside the system.console collection. The text values must be enclosed in double quotation mark. And if you have to enter other numeric values, we don’t need to put any quotation mark.
‘ (Single quotation mark) – It is used to give comments. They are not executed in the programs. It is only the help for the programmers to understand the code.
Similary system.threading.thread.sleep(2000) holds the screen for 2seconds. i.e. 2000 milliseconds before exit.
Some basic functions
Write : This function is used to trace the desired information.
Writeline : This function is also used to trace the desired information.
The difference between write and writeline is that, writeline breaks the line after it is traced. i.e. The instructions are printed in the new line.
Examples and its functions :
System.console.writeline(“Welcome”)
This statement traces the Welcome .
System.console.writeline(“your lucky number is “ & num)
In this statement, num can be integer, decimal, double etc which are numerical data types. So it prints your lucky number is and the value of num.
System.console.readline()
The function readline is used to get the input from the user.
Num = system.console.readline()
This statement asks a value from the user which is stored in the num data type.
System.Threading.Thread.sleep(5000)
This statements holds the screen from 5 seconds.
Data types in VB.net
The basic data types supported by VB.net are:-
1. Integer
2. Double
3. Boolean – 0 and 1 i.e. 0 for false and 1 for true
4. Char – stores only a character
5. String – can store 231 unicode characters
6. Single
7. byte- data range 0 to 255
8. short
9. decimal
Variables are identifiers that are used for storing data. In VB.net variables are declared using the Dim keywords.
Some rules for defining variables:
1. A variable name mush not exceed 255 character.
2. A variable name must start with an alphabet and not a number or any other character.
3. A variable name cannot contain a period(.).
Example
Module Module1
Dim name As String = "Angel" 'Store Angel to name
Dim rollno As Integer = 4 ' Store 4 to rollno
Dim sex As Char = "m" 'Store m to sex
Dim marks As Decimal = 85.25 'Store 85.25 to marks
System.Console.WriteLine("Name : " & name)
System.Console.WriteLine("Rollno : " & rollno)
System.Console.WriteLine("Sex : " & sex)
System.Console.WriteLine("Marks : " & marks)
System.Threading.Thread.Sleep(5000)
End Sub
Press F5 to see the result.
If the values of name, rollno, sex and marks are to be entered from the user then, the program can be written as:
Module Module1
Dim name As String
Dim rollno As Integer
Dim sex As Char
Dim marks As Decimal
System.Console.Write("Enter name : ")
name = System.Console.ReadLine() 'Ask for the value of name
System.Console.Write("Enter Roll no. ")
rollno = System.Console.ReadLine() 'Ask for the value of roll no
System.Console.Write("Enter sex ")
sex = System.Console.ReadLine() ' Ask for the value of sex
System.Console.Write("Enter marks")
marks = System.Console.ReadLine() 'Ask for the value of marks
System.Console.WriteLine("Name : " & name)
System.Console.WriteLine("Rollno : " & rollno)
System.Console.WriteLine("Sex : " & sex)
System.Console.WriteLine("Marks : " & marks)
System.Console.Readline() 'readline at this point help in holding the ouptut until you press enter key
End Sub
If you don’t want to waste the time writing again an again the system.console then, write imports system.console at the top of the program. i.e.
Imports system.console
Module Module1
Dim name As String
Dim rollno As Integer
Dim sex As Char
Dim marks As Decimal
Write("Enter name : ")
name = ReadLine() 'Ask for the value of name
Write("Enter Roll no. ")
rollno = ReadLine() 'Ask for the value of roll no
Write("Enter sex ")
sex = ReadLine() ' Ask for the value of sex
Write("Enter marks")
marks = ReadLine() 'Ask for the value of marks
WriteLine("Name : " & name)
WriteLine("Rollno : " & rollno)
WriteLine("Sex : " & sex)
WriteLine("Marks : " & marks)
readline() 'readline at this point help in holding the ouptut until you press enter key
End Sub
End Module