다음을 통해 공유


A Client in Visual Basic

Here is the full source-code listing for the client in Visual Basic:

Listing 1. Client in Visual Basic (ClientVB.vb)

Option Explicit
Option Strict

Imports System
Imports System.Collections

Imports CompCS
Imports CompVB
Imports CompVC

Public Module modmain

   ' The main entry point for the application.
   Sub Main()

      Dim Count As Integer

      ' Display result strings from the C# component.
      Dim MyCompCS As New CompCS.StringComponent
      Console.WriteLine("Strings from C# 
         StringComponent")
      For Count = 0 To MyCompCS.Count - 1
          Console.WriteLine(MyCompCS.GetString(Count))
      Next
      Console.WriteLine

      ' Display result strings from the Visual C++ component.
      Dim MyCompVC As New CompVC.StringComponent
      Console.WriteLine("Strings from Visual C++ 
         StringComponent")
      For Count = 0 To MyCompVC.Count - 1
          Console.WriteLine(MyCompVC.GetString(Count))
      Next
      Console.WriteLine

      ' Display the result strings from the Visual Basic component.
      Dim MyCompVB As New CompVB.StringComponent
      Console.WriteLine("Strings from Visual Basic 
         StringComponent")
      For Count = 0 To CInt(MyCompVB.Count) - 1
          Console.WriteLine(MyCompVB.GetString(Count))
      Next

   End Sub

End Module

Like the Visual C# example's using statement, the Imports statement specifies the libraries and incorporates the namespaces into the program — allowing you to reference types from the library without fully qualifying their type names. Because the example has the same type name (StringComponent) in each of the components, you still have to use the fully qualified name to remove ambiguity.

The client code is virtually identical to the Managed Extensions for C++ and Visual C# examples, except for the minor things such as the scope-resolution operators and the absence of line-termination characters. Again, the sections of code that call the three string components are also the same, except for the specification of which library to use. As with the Managed Extensions for C++ and Visual C# examples, the first statement in each of the three sections declares a new, local variable of type StringComponent, initializes the variable, and calls its constructor:

Dim MyCompCS As New CompCS.StringComponent

After writing a string to the console to indicate that this part of the program has been entered, the client uses the value of the Count property to iterate through the members of the appropriate string component:

For Count = 0 To MyCompVC.Count - 1
  Console.WriteLine(MyCompVC.GetString(Count))
Next

That is all that is required, and everything is repeated for the other two language components.

Building from the command line is quite simple. The only change is writing the component to the ..\Bin subdirectory:

vbc.exe /t:exe /debug+ /optionstrict+ 
   /reference:..\Bin\CompCS.dll 
   /reference:..\Bin\CompVB.dll 
   /reference:..\Bin\CompVC.dll 
   /out:..\bin\ClientVB.exe ClientVB.vb

See Also

A Windows Client Using Windows Forms | A Client Using ASP.NET | Summary of Development Tutorial | Appendix A: Tools for Exploring Namespaces