Tuesday, September 9, 2008

Console Application



Your Ad Here

Create a Console Application from the Command Line

You need to use the VB .NET command-line compiler to build an application that does not require
a Windows graphical user interface (GUI) but instead displays output to, and reads input from, the
Windows command prompt (console).
In one of your classes, ensure you implement a Shared method named Main with one of the following
signatures:
Public Shared Sub Main()
End Sub
Public Shared Sub Main(ByVal args As String())
End Sub
Public Shared Function Main() As Integer
End Sub
Public Shared Function Main(ByVal args As String()) As Integer
End Sub
Build your application using the VB .NET compiler (vbc.exe) by running the following command
(where HelloWorld.vb is the name of your source code file):
vbc /target:exe HelloWorld.vb
By default, the VB .NET compiler will build a console application unless you specify otherwise. For
this reason, it’s not necessary to specify the /target:exe switch, but doing so makes your intention
clearer, which is useful if you are creating build scripts that will be used by others or will be used
repeatedly over a period of time.
To build a console application consisting of more than one source code file, you must specify all
the source files as arguments to the compiler. For example, the following command builds an application
named MyFirstApp.exe from two source files named HelloWorld.vb and ConsoleUtils.vb:
vbc /target:exe /main:HelloWorld /out:MyFirstApp.exe HelloWorld.vb ConsoleUtils.vb
The /out switch allows you to specify the name of the compiled assembly. Otherwise, the assembly
is named after the first source file listed—HelloWorld.vb in the example. If classes in both the HelloWorld
and ConsoleUtils files contain Main methods, the compiler cannot automatically determine which
method represents the correct entry point for the assembly. Therefore, you must use the compiler’s
/main switch to identify the name of the class that contains the correct entry point for your application.
When using the /main switch, you must provide the fully qualified class name (including the
namespace); otherwise, you will receive the following:
If you have a lot of VB .NET code source files to compile, you should use a response file. This
simple text file contains the command-line arguments for vbc.exe. When you call vbc.exe, you give
the name of this response file as a single parameter prefixed by the @ character. Here is an example:
vbc @commands.rsp
To achieve the equivalent of the previous example, commands.rsp would contain this:
/target:exe /main:HelloWorld /out:MyFirstApp.exe HelloWorld.vb ConsoleUtils.vb
For readability, response files can include comments (using the # character) and can span multiple
lines. The VB .NET compiler also allows you to specify multiple response files by providing multiple
parameters that are prefixed with the @ character.
The Code
The following code lists a class named ConsoleUtils that is defined in a file named ConsoleUtils.vb:
Imports System
Namespace Apress.VisualBasicRecipes.Chapter01
Public Class ConsoleUtils
' This method will display a prompt and read a response from the console.
Public Shared Function ReadString(ByVal message As String) As String
Console.Write(message)
Return Console.ReadLine
End Function
' This method will display a message on the console.
Public Shared Sub WriteString(ByVal message As String)
Console.WriteLine(message)
End Sub
' This method is used for testing ConsoleUtility methods.
' While it is not good practice to have multiple Main
' methods in an assembly, it sometimes can't be avoided.
' You specify in the compiler which Main sub routine should
' be used as the entry point. For this example, this Main
' routine will never be executed.
Public Shared Sub Main()
' Prompt the reader to enter a name.
Dim name As String = ReadString("Please enter a name: ")
' Welcome the reader to Visual Basic 2008 Recipes.
WriteString("Welcome to Visual Basic 2008 Recipes, " & name)
End Sub
End Class
End Namespace
The HelloWorld class listed next uses the ConsoleUtils class to display the message “Hello,
World” to the console (HelloWorld is contained in the HelloWorld.vb file):
Imports System
Namespace Apress.VisualBasicRecipes.Chapter01
Public Class HelloWorld
Public Shared Sub Main()
ConsoleUtils.WriteString("Hello, World")
ConsoleUtils.WriteString(vbCrLf & "Main method complete. Press Enter.")
Console.ReadLine()
End Sub
End Class
End Namespace
To build HelloWorld.exe from the two source files, use the following command:
vbc /target:exe /main:Apress.VisualBasicRecipes.Chapter01.HelloWorld ➥
/out:HelloWorld.exe ConsoleUtils.vb HelloWorld.vb

0 comments: