A Simple Component


At this point it will be useful to work through a simple example before presenting any more theory. The following program creates a component called CipherComp that implements a very simple encryption strategy. A character is encoded by adding 1 to it. It is decoded by subtracting 1. To encrypt a string, call Encode( ), passing the plaintext as an argument. To decipher an encrypted string, call Decode( ), this time passing the ciphertext as an argument. In both cases, the translated string is returned.

 // A simple Cipher component.  Call this file CipherLib.cs. using System.ComponentModel; namespace CipherLib { // put component in its own namespace   // Notice that CipherComp inherits Component.   public class CipherComp : Component {     // Encode a string.     public string Encode(string msg) {       string temp = "";       for(int i=0; i < msg.Length; i++)         temp += (char) (msg[i] + 1);       return temp;     }     // Decode a string.     public string Decode(string msg) {       string temp = "";       for(int i=0; i < msg.Length; i++)         temp += (char) (msg[i] - 1);       return temp;     }   } }

Let’s examine this code closely. First, as the comment at the top of the file suggests, to follow along with the example, call the file CipherLib.cs. This makes it easier to use the component if you are using the Visual Studio IDE. Next, notice that System.ComponentModel is included. As explained, this is the namespace that supports component programming.

The CipherComp class is enclosed within a namespace called CipherLib. Putting a component within its own namespace prevents the global namespace from becoming cluttered and is a good practice to follow. It is not technically necessary, though.

The CipherComp class inherits Component. This means that CipherComp fulfills the contract necessary to be a .NET-compatible component. Since CipherComp is very simple, it does not need to perform any component-related functions on its own. Component handles all the clerical details.

Next, notice that CipherComp does not allocate any resources. Specifically, it does not hold any references to any other objects. It simply defines two methods called Encode( ) and Decode( ). Because CipherComp does not hold any resources, it does not need to implement Dispose(bool). Of course, both Encode( ) and Decode( ) return string references, but these references are owned by the calling code and not by the CipherComp object.

Compiling CipherLib

A component must be compiled into a dll rather than an exe file. If you are using the Visual Studio IDE, you will want to create a Class Library project for CipherLib. When using the command-line compiler, you will specify the /t:library option. For example, to compile CipherLib, you can use this command line:

 csc /t:library CipherLib.cs

This creates a file called CipherLib.dll, which contains the CipherComp component.

A Client That Uses CipherComp

After you have created a component, it is ready for use. For example, the following program is a client of CipherComp, which it uses to encode and decode a string:

 // A client that uses CipherComp. using System; using CipherLib; // import CipherComp's namespace class CipherCompClient {   public static void Main() {     CipherComp cc = new CipherComp();     string text = "This is a test";     string ciphertext = cc.Encode(text);     Console.WriteLine(ciphertext);     string plaintext = cc.Decode(ciphertext);     Console.WriteLine(plaintext);     cc.Dispose(); // free resources   } }

Notice that the client includes the CipherLib namespace. This brings the CipherComp component into view. Of course, it would have been possible to fully qualify each reference to CipherComp, but including its namespace is easier. Next, CipherComp is used like any other class.

Notice the call to Dispose( ) at the end of the program. As explained, by calling Dispose( ), the client causes the component to free any resources that it might be holding. Although components use the same garbage collection mechanism used by any other type of C# object, garbage collection is performed only sporadically. By calling Dispose( ), you cause the component to release its resources immediately. This can be important in certain situations, for instance, when a component holds a limited resource, such as a network connection. Because CipherComp does not hold any resources of its own, the call to Dispose( ) is not actually needed in this example. However, because Dispose( ) is part of the component contract, it is a good idea for you to get into the habit of calling it when you are done using a component.

To compile the client program, you must tell the compiler to reference CipherLib.dll. To do this, use the /r option. For example, the following command line compiles the client program:

 csc /r:CipherLib.dll client.cs

If you are using the Visual Studio IDE, you will need to add CipherLib.dll as a reference to the client.

When you run the program, you will see the following output:

 Uijt!jt!b!uftu This is a test




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net