Console Applications


In that the purpose of the next several topics is to explore the basics of the VB.NET language, the GUIs for the sample programs presented in those topics will be quite minimal. The sample programs in those topics (which will be referred to as "Try It" programs) will use the Windows Console (command-line) interface.


A Console Application project is easy to set up; the steps to do this will be explored below. Console applications use the .NET Console class. The "Try It" sample programs will use of the Read or ReadLine methods for input and the Write or WriteLine methods for output. These methods are described briefly in the table below:





Reads information from command-line (or other input). Does not require the line to end with Enter.


Reads information from the command-line (or other input). Reads all characters up to, but not including Enter.


Writes information to the command line (or other output). Does not end with a new line.


Writes information to command line (or other output). Ends output with a new line.


Other useful methods of the Console class include the following:





Changes the destination for error messages displayed while your program is running. This could be used to create a simple error logging mechanism for your application.


Changes the source of input for Read and ReadLine. This could be used to change the command-line to read from a file or network.


Changes the destination for Write and Writeline methods. This could be used to change the destination for output to a file for logging or other use.


The steps for creating a VB.NET Console Application are illustrated below. The sample application built here serves as a model for the subsequent "Try It" sample programs.


1.         Start up VB 2010 Express and click the New Project link:




2.         In the New Project dialog box, select Console Application. If desired, give the project a name other than the default "ConsoleApplication1" (I'm using "03-07-ConsoleApp" for my purposes). Then click OK.




3.         The IDE should look similar to the screen shot below. Note that Module1.vb has been added to the project, and that a stub for the Sub Main procedure has been added to Module1.  A Console Application requires that "Sub Main" be the startup object. This means that there must be a Module in your VB.NET project which contains a Sub procedure named Main. The execution of the application will begin with the code contained in the Sub Main procedure.



4.         Type in the following code in between the lines Sub Main() and End Sub:


  Dim strName As String


        Console.Write("Enter Your Name: ")

        strName = Console.ReadLine


        Console.WriteLine("Hello " & strName)



            The Module1.vb tab of the IDE should look like this:




            An explanation of the code is as follows:


Dim strName As String

declares a String variable called strName

Console.Write("Enter Your Name: ")

Writes the prompt "Enter Your Name: " to the screen.

strName = Console.ReadLine

Reads the user's input and stores it in the variable strName


Writes a blank line to the screen

Console.WriteLine("Hello " & strName)

Writes a line containing the literal "Hello " concatenated with the content of the strName variable to the screen


Waits for the user to press Enter


5.         Run the application by clicking the right-pointing triangle button on the IDE toolbar:



            Once you click the run button, a console window will open, prompting you for your name:




            Upon typing your name and pressing Enter, the program will echo back the message "Hello" followed by your name:



            You must press Enter once more to end the program, which will return you to the IDE.


6.         Click the Save All button to save the project.



7.         Save the project to the desired location.




8.         Exit VB 2010 Express.



The above example can be used as a model for the subsequent "Try It" sample programs that will follow. Simply open a new Console application, copy the recommended code into the Sub Main procedure, and run. When done, you can save the project with your desired name in your projects location folder.


Download the files for the example here