I have divided this tutorial in two parts.
- Building a server class library.
- Building a consumer client application.
- Building a Server Class Library.
Create an Empty Class Library Project
Select File->New->Project->Visual Basic Projects->Class Library.
Select your project name and appropriate directory using Browse button and click OK. I give my component name "VBNetMathComponent" as you can see from Figure 1.
Figure 1.
Project and Its files
The Solution Explorer adds two Visual Basic classes to your project. First is AssemblyInfo.vb and second is Class1.vb. We don't care about AssemblyInfo class. We will be concentrating on Class1.vb. See Figure 2.
Figure 2.
If you double click on Class1.vb, you will see the class declaration as following:
Public Class Class1End Class
Here you can change your class name to whatever you want. We change our class name to McMathClass. Now my new class looks like the following:
Public Class McMathClassEnd Class
Adding Methods and Properties to the Class
Now we will add one method and one property to the class. We define a public function called Add, which takes two input parameters and returns the sum of two. We also add a property called AllowAddition of type Boolean. The Add method works only when this property is true.
The source code for Add method and AllowAddition property is listed as following:
Public Class McMathClass' Define a local variable to store the property value.Private allowSum As Boolean' Define Add function which adds two double values and retunes the ' sum of twoPublic Function Add(ByVal val1 As Double, ByVal val2 As Double) As Double If allowSum = True Then' Return the sum of two input valuesReturn val1 + val2ElseReturn 0End IfEnd Function' Define AllowAddition propertyPublic Property AllowAddition() As StringGetReturn allowSumEnd GetSet(ByVal Value As String)allowSum = ValueEnd SetEnd PropertyEnd Class
Build the DLL
Now build the DLL and see bin\debug directory of your project. You will see VBNetMathComponent.dll in this folder. This is your component.
Building a Class Library Consumer Client Application
Now we will create a client application that will call our class library and its members. We will create a console application that will consume our class library.
Create a Console Application
Again, we create the console application using Visual Studio .NET.
Select File->New->Project->Visual Basic Projects->Console Application.
I give my console application name MathLibraryConsumer as you can see from the Figure 3.
Figure 3.
Add Reference to the Library
Now next step is to add reference to the library. You can use Add Reference menu option to add a reference. Go to Project->Add reference and browse for VBNetMathComponent.dll as you can see from Figure 4.
Figure 4.
After completing this step, you will notice the component reference is added to your project references as you can see from Figure 5.
Figure 5.
Calling the Component
Now we create an instance of McMathClass, set its property and call Add method. As you can see from the following code, first I set AllowAddition property to false and call the Add method and send the output to the console. After that I set AllowAddition proeprty to true, call the Add method again, and send the output to the cosnole.
Module Module1Sub Main()' Define a private variableDim total As Double' Create an instance of McMathClassDim myComp As New VBNetMathComponent.McMathClass()' Set AllowAddition property to falsemyComp.AllowAddition = FalseConsole.WriteLine("Adding two numbers with AllowAddition False:")' Add two numberstotal = myComp.Add(3, 5)' Send output to consoleConsole.WriteLine(total.ToString())' Now set AllowAddition to truemyComp.AllowAddition = TrueConsole.WriteLine("Adding two numbers with AllowAddition True:")' Add two numberstotal = myComp.Add(3, 5)' Send output to consoleConsole.WriteLine(total.ToString())Console.ReadLine()End SubEnd Module
Now build and run the project. The output looks like Figure 6. As you can see from this output, the Add method returns 0 when the AllowAddition property is false, and returns the sum when the AllowAddition property is true.
Figure 6.
http://www.vbdotnetheaven.com/UploadFile/mahesh/BuildingClassLibrary04262005091233AM/BuildingClassLibrary.aspx?ArticleID=73c74933-cc1b-49db-b90a-d459f64a0781
Tidak ada komentar:
Posting Komentar