How to: Create and Use Assemblies Using the Command Line (Visual Basic)

An assembly, or a dynamic linking library (DLL), is linked to your program at run time. To demonstrate building and using a DLL, consider the following scenario:

  • MathLibrary.DLL: The library file that contains the methods to be called at run time. In this example, the DLL contains two methods, Add and Multiply.

  • Add: The source file that contains the method Add. It returns the sum of its parameters. The class AddClass that contains the method Add is a member of the namespace UtilityMethods.

  • Mult: The source code that contains the method Multiply. It returns the product of its parameters. The class MultiplyClass that contains the method Multiply is also a member of the namespace UtilityMethods.

  • TestCode: The file that contains the Main method. It uses the methods in the DLL file to calculate the sum and the product of the run-time arguments.


' File: Add.vb   
Namespace UtilityMethods  
    Public Class AddClass  
        Public Shared Function Add(ByVal i As Long, ByVal j As Long) As Long  
            Return i + j  
        End Function  
    End Class  
End Namespace  
' File: Mult.vb  
Namespace UtilityMethods  
    Public Class MultiplyClass  
        Public Shared Function Multiply(ByVal x As Long, ByVal y As Long) As Long  
            Return x * y  
        End Function  
    End Class  
End Namespace  
' File: TestCode.vb  
Imports UtilityMethods  
Module Test  
    Sub Main(ByVal args As String())  
        System.Console.WriteLine("Calling methods from MathLibrary.DLL:")  
        If args.Length <> 2 Then  
            System.Console.WriteLine("Usage: TestCode <num1> <num2>")  
        End If  
        Dim num1 As Long = Long.Parse(args(0))  
        Dim num2 As Long = Long.Parse(args(1))  
        Dim sum As Long = AddClass.Add(num1, num2)  
        Dim product As Long = MultiplyClass.Multiply(num1, num2)  
        System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum)  
        System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product)  
    End Sub  
End Module  
' Output (assuming 1234 and 5678 are entered as command-line arguments):  
' Calling methods from MathLibrary.DLL:  
' 1234 + 5678 = 6912  
' 1234 * 5678 = 7006652  

This file contains the algorithm that uses the DLL methods, Add and Multiply. It starts with parsing the arguments entered from the command line, num1 and num2. Then it calculates the sum by using the Add method on the AddClass class, and the product by using the Multiply method on the MultiplyClass class.

Notice that the Imports statement at the beginning of the file enables you to use the unqualified class names to reference the DLL methods at compile time, as follows:

MultiplyClass.Multiply(num1, num2)  

Otherwise, you have to use the fully qualified names, as follows:

UtilityMethods.MultiplyClass.Multiply(num1, num2)  


To run the program, enter the name of the EXE file, followed by two numbers, as follows:

TestCode 1234 5678

See also