O'Reilly logo

Object-Oriented Programming with Visual Basic .NET by J.P. Hamilton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.3. Using a Component

Now that you have a component, you need a way to use it. Example 2-2 contains a listing for a simple client. Save it to a file named hello-client.vb. Let's see how everything fits together, and then you can compile it.

Example 2-2. "Hello, world" client
Imports System
Imports Greeting
   
Public Class Application
   
  Public Shared Sub Main( )
    Dim hw As New Hello( )
    hw.Write("World")
    Console.ReadLine( )
  End Sub
   
End Class

The Greeting namespace defined in Example 2-1 was imported. Without it, every class in the Greeting namespace would have to be referenced directly, as in the following code:

Public Shared Sub Main( )
  Dim hw As New Greeting.Hello( )
  hw.Write("World")
End Sub

All standalone executables require an entry point with this signature:

Public Shared Sub Main( )

This is where everything begins, but as you can see, not much is going on. The only thing the client does is declare an instance of the Hello class (which is defined in the component) and call its Write method.

When compiled, Example 2-2 must explicitly reference hello.dll for everything to compile. This can be accomplished with the /r compiler option. Assuming that the DLL lives in the same directory as the client code, this executable can be compiled as follows:

C:\>vbc /t:exe /r:hello.dll hello-client.vb

This compilation produces an executable named hello-client.exe. You can change the name of the output file by using the /out compiler option:

C:\>vbc /t:exe /r:hello.dll /out:hello.exe hello-client.vb ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required