Hello VB.NET

This short article is based on the classic "Hello, World”" sample program familiarizes you with the language syntax and introduces the command line compiler. It illustrates how to write a VB program and compile it from the command line by exploring some of the basics of VB programming and compiler options. After completing this article, you will k
  • 2426

VB.NET Compiler

Although we assume you are using Visual Studio .NET, there is another way for you to get the VB compiler and compile your programs from the command line. The command line VB compiler comes along with the .NET Framework Software Development Kit (SDK). The URL for retrieving these files can be found in the Downloads section of the Web site VBDotNetHeaven (http://www.vbdotnetheaven.com). 

You can use any text editor to type VB programs. After typing the code, save it with the .vb extension and compile it from the command line using the VB compiler called csc. 

Listing 3.1 shows the Hello, VB program. Type this program in a text editor such as Notepad or in the Visual Studio .NET editor. The output of the program in Listing 1 to the system console produce "Hello, VB!" after it is compiled and run. 

Listing 1: "Hello, VB" Example 

Imports System
Module Module1

    Sub Main()
        Console.WriteLine("Welcome to VB.NET")
    End Sub

End Module

Save the file as hello.vb and compile it from the command line using the VB compiler, using the following syntax:

csc hello.vb or C:\temp> csc hello.vb 

Make sure the file path is correct. 

If you don't find csc.exe in your current directory, provide the full path of csc.exe. For example, if your csc.exe is in C:\Program Files\.NET\Exes\, then compile your code from the command prompt using this command: 

C:\Program Files\.NET\Exes\csc.exe C:\hello.vb

After compiling your code, the VB compiler creates an executable file called hello.exe in the bin directory. Figure 3.1 shows the executable file as it is running. 

image1.gif

Figure 1: "Hello VB" Output in Command Line 

Description of "Hello, VB" 

The first line of your program is using System. 

Imports System

The next line of code may consist of the static void Main() function, but this depends on what form of the Main method you are using. The different forms of the Main method are discussed later in this article. 

In VB every application must include a Sub Main()  method. This method is similar to the main() function of Vb. This is the entry point of an application. Although an application can have multiple Main methods, you can assign only one of them as the application's entry point. For example: 

    Sub Main()
        Console.WriteLine("Hello VB!")
 
    End Sub

The next line of code uses the System.Console class to write output to the console. WriteLine(), a method of the Console class, writes a string followed by a line terminator to the console. The Console class defined in the System namespace contains methods to read and write from the console. 

Compiler Options 

When you compile a VB program, the compiler doesn't generate a native (executable) file that is executed directly by the system; rather, it generates a Common Intermediate Language file, which is read by the Common Language Runtime at runtime and then executed. Table 1 lists several compiler options you can use in your command line along with the VB compiler and describes what they do. 

image2.JPG

Table 1: Compiler Command Line Options 

The Multiple Faces of the Main Method 

The Main method has a few different declarations from which you can choose. Some of its declarations are discussed here. 

The first version of the Main method is the void type. This method contains no return and no arguments. The second version of the Main method has no returns but takes arguments, and the third type returns an integer value but no arguments. The different versions of the Main method are shown in Listing 2. 

Listing 2: Different Versions of the Main Method 

    ' static Main no return, no arguments 
    Private Sub Main()
        Console.WriteLine("Hello, VB!")
    End Sub
    ' static Main no return, arguments 
    Public Sub Main(ByVal args As String())
        Console.WriteLine("Hello, VB!")
    End Sub
    ' static Main no return, arguments 
    Public Function Main() As Integer
        Console.WriteLine("Hello, VB!")
        Return 0
    End Function

Command Line Parameters 

One form of the Main method plays a vital role when you need to process command line parameters. This Main method takes an argument of a string array. For example: 

    ' static Main no return, arguments

    Public Sub Main(ByVal args As String())
        Console.WriteLine("Hello, VB!")
    End Sub

Let's look at a sample of passing a string array and writing out the arguments. Listing 3 shows such an example. 

Listing 3 "Hello.vb" Command Line Arguments 

Imports System
Module Module1

     Public Sub Main(ByVal args As String())
        For i As Integer = 0 To args.Length - 1
            Console.Write("Args {0}, {1}", i, args(i))
            Console.Write(vbLf)
        Next
    End Sub

End Module

Another interesting example presented in Listing 4 takes three arguments. The first argument is either Add or Subtract, and the second and third arguments are two numbers. If the first argument is Add, the output is the sum of two numbers. If the first argument is Subtract, the output is the difference of two numbers. 

Listing 4: Add and Remove Arguments from Command Line 

Imports System
Module Module1

    ' static Main no return, arguments 
    Public Sub Main(ByVal args As String())
        Dim num1 As Integer = Convert.ToInt16(args(1))
        Dim num2 As Integer = Convert.ToInt16(args(2))
        Dim result As Integer
        If args(0) = "Add" Then
            result = num1 + num2
            Console.WriteLine(result.ToString())
        End If

        If args(0) = "Subtract" Then
            result = num1 - num2
            Console.WriteLine(result.ToString())
        End If
    End Sub

End Module

Referencing an Assembly from the Command Line 

The core functionality of the System namespace resides in mscorlib.dll, which is available to a program without referencing it. Thus, if you are using only the System namespace, you do not have to pass any assembly information as a command line argument. However, if you are using any other assemblies such as System.Drawing or System.Windows, you need to pass the assembly information as an argument. 

For example, if you are using the Windows.Forms namespace in your program, you need to reference the system.winforms.dll assembly as a command line argument. 

csc /r:system.winforms.dll myforms.vb

The VB compiler comes along with the .NET Framework SDK. You can install the VB compiler in one of two ways: either install the .NET SDK or install VS.NET. Writing a "Hello, World" sample in VB is quite similar to writing one in C++ or Java. The Main method is the entry point of any application. You can pass your command line arguments in the Main method and use them in your application.

Conclusion

Hope this article would have helped you in writing a simple Hello VB program using VB coding. See my other articles on the website on .NET and VB.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.