Here's the full source code listing for our client in VB:
Listing 9. Client in Managed VB (Component1.cls)
Imports System Imports System.Collections Imports CompCS Imports CompVB Imports CompVC Option Explicit Public Module modmain 'The main entry point for the application Sub Main() Dim Count As Integer ' Display result strings from 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 Visual C++ Component Dim MyCompVC As New CompVC.StringComponent Console.WriteLine("Strings from VC StringComponent") For Count = 0 To MyCompVC.Count - 1 Console.WriteLine(MyCompVC.GetString(Count)) Next Console.WriteLine ' Display result strings from Visual Basic Component Dim MyCompVB As New CompVB.StringComponent Console.WriteLine("Strings from VB StringComponent") For Count = 0 To CInt(MyCompVB.Count) - 1 Console.WriteLine(MyCompVB.GetString(Count)) Next End Sub End Module
Like the C# example's using
statement, we specify the libraries with the Imports
statement that then incorporate the namespace into the program so we can reference types in the library without fully qualifying their type names. Since our particular example has the same type name (StringComponent
) in each of the components, we still have to use the fully qualified name to remove any ambiguity.
The client code is virtually identical to the C++ and C# examples except for the minor things such as the scope resolution operators and absence of a line termination character. Again, the code that calls the three string components is also the same except for specifying which library to use. As with the MC++ and C# examples, the first statement in each of the three sections declares a new local variable of a type StringComponent
, initializes it, and calls its constructor:
Dim MyCompCS As New CompCS.StringComponent
After writing out a string to the console to say we're entering this part of the program, the client then iterates over the members – up to the value of the Count
property – of appropriate string component:
For Count = 0 To MyCompVC.Count - 1 Console.WriteLine(MyCompVC.GetString(Count)) Next
That's all that's required, and everything's repeated for the other two language components.
The command-line build is quite simple, the only change being to output the component to the relative "..\Bin" subdirectory:
vbc ClientVB.vb /reference:..\Bin\CompCS.dll /reference:..\Bin\CompVB.dll /reference:..\Bin\CompVC.dll /out:..\bin\ClientVB.exe /t:exe