Apart from Windows applications, you can use Visual Studio 2008 to build applications that run in a command prompt window. The command prompt window isn’t really a DOS window, even though it looks like one. It’s a text window, and the only way to interact with an application is to enter lines of text and read the output generated by the application, which is displayed in this text window, one line at a time. This type of application is called a console application, and I’m going to demonstrate console applications with a single example. We will not return to this type of application later in the tutorial because it’s not what you’re supposed to do as a Windows developer.
The console application you’ll build in this section, ConsoleApplication1, prompts the user to enter the name of her favorite language. It then prints the appropriate message on a new line, as shown in Figure 1.16.
Start a new project. In the New Project dialog box, select the template Console Application. You can also change its default name from ConsoleApplication1 to a more descriptive name. For this example, don’t change the application’s name. A console application doesn’t have a user interface, so the first thing you’ll see is the code editor’s window with the following statements:
Module Module1 Sub Main() End Sub End ModuleCode language: VB.NET (vbnet)
Unlike a Windows application, which is a class, a console application is a module. Main() is the name of a subroutine that’s executed automatically when you run a console application. The code you want to execute must be placed between the statements Sub Main() and End Sub. Insert the statements shown in Listing 1.3 in the application’s Main() subroutine.
Listing 1.3: Console Application
Module Module1 Sub Main() Console.WriteLine(”Enter your favorite language”) Dim language As String language = Console.ReadLine() language = language.ToUpper If language = ”VISUAL BASIC” Or language = ”VB” Or language = ”VB.NET” Then Console.WriteLine(”Wow! you selected ” & language) Else Console.WriteLine(language & ”is not a bad language.”) End If Console.WriteLine() Console.WriteLine() Console.WriteLine(”PRESS ENTER TO EXIT”) Console.ReadLine() End Sub End ModuleCode language: VB.NET (vbnet)
This code is quite similar to the code of the equivalent Windows applications we developed earlier, except that it uses the Console.WriteLine statement to send its output to the command prompt window instead of a message box.
A console application doesn’t react to events because it has no visible interface. However, it’s easy to add some basic elements of the Windows interface to a console application. If you change the Console.WriteLine method call into the MsgBox() function, the message will be displayed in a message box.
The reason to build a console application is to test a specific feature of the language without having to build a user interface. Many of the examples in the documentation are console applications; they demonstrate the topic at hand and nothing more. If you want to test the DateDiff() function, for example, you can create a new console application and enter the lines of Listing 1.4 in its Main() subroutine.
Listing 1.4: Testing the DateDiff() Function with a Console Application
Sub Main() Console.WriteLine(DateDiff(DateInterval.Day, Console.WriteLine("PRESS ENTER TO EXIT") Console.ReadLine() End SubCode language: VB.NET (vbnet)
The last two lines will be the same in every console application you write. Without them, the command prompt window will close as soon as the End Sub statement is reached, and you won’t have a chance to see the result. The Console.ReadLine method waits until the user presses the Enter key.
Console applications are convenient for testing short code segments, but Windows programming is synonymous with designing graphical user interfaces, so you won’t find any more console applications in this tutorial.