Visual Basic.NET 2008 > Getting Started with VB.NET

Building a Console Application in Visual Basic 2008

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.

Visual Basic 2008 - Console Application Example

Figure 1.16 - A console application in developed in Vb 2008 which uses the command prompt window to interact with the user.

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 Module

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 Module

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, #3/9/2005#, #5/15/2010#))
Console.WriteLine("PRESS ENTER TO EXIT")
Console.ReadLine()
End Sub

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.

 

Table of Contents

  1. Getting Started with Visual Basic.NET 2008
  2. The Integrated Development Environment - IDE of VB 2008
  3. Starting a New Project in Visual basic 2008
  4. Using the Windows Form Designer - VB 2008
  5. Creating Your First VB Application
  6. Making Your VB Application More User-Friendly
  7. Understanding the IDE Components in VB.NET 2008
  8. Toolbox Window, Solution Explorer Window, Properties Window, Output Window in VB.NET
  9. Command and Immediate Windows, Error List Window in VB 2008
  10. Setting Environment Options in Visual Basic 2008
  11. Building a Console Application in VB.NET 2008
  12. VB.NET - Using Code Snippets
  13. Using the MyObject in Visual Basic 2008

     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us