다음을 통해 공유


A Client in Visual C#

Here is how the client looks in Visual C#:

Listing 1. Client in Visual C# (ClientCS.cs)

using System;

using CompVC;
using CompCS;
using CompVB;

// This class exists to house the application's entry point.
class MainApp {
  // The static method, Main, is the application's entry point.
  public static void Main() {

    // Iterate through the component's strings,
    // and write them to the console.
    CompCS.StringComponent myCSStringComp = new 
        CompCS.StringComponent();
    Console.WriteLine("Strings from C# StringComponent");
    for (int index = 0; index < myCSStringComp.Count; 
        index++) {
      Console.WriteLine(myCSStringComp.GetString(index));
    }

    // Iterate through the component's strings,
    // and write them to the console.
    CompVC.StringComponent myVCStringComp = new 
        CompVC.StringComponent();
    Console.WriteLine("\nStrings from Visual C++ 
       StringComponent");
      for (int index = 0; index < myVCStringComp.Count; 
         index++) {
       Console.WriteLine(myVCStringComp.GetString(index));
      }

    // Iterate through the component's strings,
    // and write them to the console.
    CompVB.StringComponent myVBStringComp = new 
        CompVB.StringComponent();
    Console.WriteLine("\nStrings from Visual Basic 
        StringComponent");
    for (int index = 0; index < myVBStringComp.Count; 
        index++) {
      Console.WriteLine(myVBStringComp.GetString(index));
    }
  }
}

Unlike in the Managed Extensions for C++ example, you do not have to import the libraries at this point. Instead, you can specify them in the compile process. The advantage of specifying a library through the using statement is that using incorporates the namespace into the program — allowing you to reference types in the library without fully qualifying the type names. Since all the sample components have the same type name (StringComponent), you still have to use the fully qualified name to remove any ambiguity when referring to the method (GetString) and the property (Count), which are common to all components.

Visual C# also provides a mechanism called aliasing to address this problem. If you changed the using statements to the following ones, you would not have to fully qualify the names:

using VCStringComp = CompVC.StringComponent;
using CSStringComp = CompCS.StringComponent;
using VBStringComp = CompVB.StringComponent;

The client code is virtually identical to the Managed Extensions for C++ example, except for the scope-resolution operators. Again, the code that calls the three string components is also the same, except for the specification of which library to use. As with the Managed Extensions for C++ example, the first statement in each of the three sections declares a new, local variable of type StringComponent, initializes the variable, and calls its constructor:

CompCS.StringComponent myCSStringComp = 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 (int index = 0; index < myVCStringComp.Count; 
    index++) {
   Console.WriteLine(myVCStringComp.GetString(index));
}

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

Building the new Visual C# client is straightforward. Now that you are using components from your own ..\Bin subdirectory, you need to explicitly include them using the /reference compilation switch:

csc.exe /debug+ /reference:..\Bin\CompCS.dll; 
   ..\Bin\CompVB.dll;..\Bin\CompVC.dll  
   /out:..\Bin\ClientCS.exe ClientCS.cs

Also, since you are building a client program rather than a component that might be called from other programs, it is not necessary to use the /t switch.

Other than that, the process is the same as in the previous Visual C# examples. Running the resulting program yields the following:

C:\...\CompTest\Bin>clientcs
Strings from C# StringComponent
C# String 0
C# String 1
C# String 2
C# String 3

Strings from Visual C++ StringComponent
Visual C++ String 0
Visual C++ String 1
Visual C++ String 2
Visual C++ String 3

Strings from Visual Basic StringComponent
Visual Basic String 0
Visual Basic String 1
Visual Basic String 2
Visual Basic String 3

See Also

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