Writing Console Applications in .Net - ' Structure of ' (
Page 3 of 4 )
.Net Console Apps">
Structure of .Net Console Applications
When you create a new console application, Visual Studio creates a module with one procedure in it, named Main().
ADVERTISEMENT
Main() is where execution starts. You will add code to Main(), of course, as well as adding additional procedures and even additional modules if needed. Your code has access to the full .Net class library. Naturally, you will not use any framework elements that require visual display.
Module Module1
Sub Main()
End Sub
End Module
The Console Class
At the heart of any console application is the Console class. This class is part of the System namespace, and it provides members for reading from the standard input stream and writing to the standard output stream. The class members that your use for input and output are described here:
Read(): Returns a type Integer representing the next character available from the standard input. Returns -1 if no character is available.
ReadLine(): Returns a string containing all the characters from standard input up to, but not including, the next carriage return (CR), line feed (LF), or CR/LF combination.
Write(exp): Writes exp to the standard output.
WriteLine(exp): Writes exp followed by a newline character to the standard output.
When inputting data from the keyboard, the Read() method does not return until the user presses Enter. Then, repeated calls to Read() return the numerical codes (ASCII) for the typed characters, one at a time, up to and including the codes for the CR/LF characters that represent pressing Enter (13 for the carriage return, 10 for the line feed). This is demonstrated by the console application the following listing. When you run this program, you will see how the characters you enter are input one at a time by the Read() method. Sample program output is shown in Figure 12.1.
Sub Main()
Dim i As Integer
Console.WriteLine("Enter some text then press Enter:")
Do While True
i = Console.Read
Console.WriteLine(i)
If i = 10 Then Exit Do
Loop
Console.Write("Press Enter to end program.")
Console.Read()
End Sub
Command Line Arguments
You can pass information to a console application when it is started by means of command-line arguments. A command-line argument is placed after the program name on the command line. For example, you could start the console application MyConsoleApp by typing the following at the command prompt:
myconsoleapp -c data.txt
The -c and the data.txt are command-line arguments and are available to code within the program. You can use command-line arguments to pass information to the program about what actions it is to carry out, what files it is to work with, and so on. Command-line arguments are separated from the program name and from one another by spaces. If an argument contains a space, enclose it in quotes, as in myconsoleapp -c "c:\my documents\data.txt". This permits the second argument to be treated as a single argument rather than as two separate arguments.
You use the Command() function to access command-line arguments in your code. This function returns a string containing all the text that was entered on the command line after the program name. It does not parse out the individual arguments; the program must do this by looking for spaces and double quotes in the returned string and extracting the individual arguments. You can find an example of this in the VB.Net online help in the section on the Command() function.
When testing a console application in the Visual Studio environment, you can specify its command line arguments. By default, a console application runs without command-line arguments when executed from within Visual Studio. To add one or more arguments, follow these steps:
In the Solution Explorer, select the project.
Select Property Pages from the View menu, or press Shift+F4, to open the project's property page.
On the left side of the property page, select Debugging under Configuration Properties.
In the box labeled Command Line Arguments, enter one or more arguments separated by spaces, remembering to enclose an argument in double quotes if it contains a space.
Click OK. When you run the project, the specified arguments will be passed to your program just as if they had been entered at the command prompt.