Using Visual C # For components is actually a very easy task. At least it is much easier than using visual c ++. In this article, I will introduce you in detail how to make a component. We can do this through two steps: 1. Make a component; 2. Test the component on the client.
1. Create a component
1. First create a new class library project file
Choose File> New> Project> Visual C # Projects> Class Library. Enter the project file name and click the Browse button to select the directory where the file is to be stored. Click OK.
2. engineering files and Their contained files
At this time, the Solution Explorer in the project file has added two C # classes. They are AssemblyInfo. cs and Class1.cs. We only need to care about Class1.cs. AssemblyInfo. cs can be ignored.
3. namespace mcMath (namespace mcMath)
Double-click Class1.cs and you will see the namespace mcMath. This namespace will be involved when the client calls the component:
Public long Add (long val1, long val2)
{
Return 0;
}
Public bool Extra
{
Get
{
Return true;
}
Set
{
}
}
So far, after confirming that all the above operations have been completed, you can find mcMath. dll in the bin/debug directory when compiling this project file, which is a component. However, the component does not have any function at this time. We will further improve it below.
4. Add a method
Open ClassView from the View menu. We can see that Class1 does not have any methods and attributes. Now we add a method and an attribute to it.
Click Class1, right-click, and select Add-> Add Method...
The visual c # method creation wizard window is displayed. With the help of this wizard, you can add the method name, access type, return value, parameters, and even comments to your component. You can use the Add and Remove buttons to conveniently Add or delete parameters from the parameter list. Here we Add a method: long Add (long val1, long val2). The function of this method is to Add two numbers and then return their sum.
5. Add an attribute
Repeat the process of adding a method. In the same position, open the C # Attribute wizard and add an attribute for the component.
After the above process is completed, Class1 becomes the following:
Take a closer look at the Class1 class and you will find that the wizard has added two functions to our components:
Public long Add (long val1, long val2)
{
Return 0;
}
Public bool Extra
{
Get
{
Return true;
}
Set
{
}
}
6. Add code for the Class
Modify the Class according to the following code and rename Class1 to mcMathComp (this is because Class1 is the default name, which may cause confusion and may cause problems when the client calls components ).
Namespace mcMath
{
Using System;
Public class mcMathComp
{
Private bool bTest = false;
Public mcMathComp ()
{
}
Public long Add (long val1, long val2)
{
Return val1 + val2;
}
Public bool Extra
{
Get
{
Return bTest;
}
Set
{
BTest = Extra;
}
}
}
}
7. generate DLL
Compile the project file generation component, which will be in the bindebug directory of the project file. The file extension is DLL.
Ii. Test DLL
On the client, use visual c # To Call The component we just generated. Follow these steps.
1. Create a new control application
Choose File> New> Project> Visual C # Projects> Console Application. We use this control application to test our components.
2. Add reference for Namespace
Open Project-> Add reference, browse the generated DLL, and press OK.
The add reference wizard adds the reference to the class of the current project file.
3. Call the mcMath namespace, create the mcMathComp object, and call its methods and attributes.
Follow the steps below to conveniently call methods and attributes.
(1) Use namespace and mcMath;
(2) create a mcMathComp object;
McMathComp cls = new mcMathComp ();
(3) Call methods and properties;
McMathComp cls = new mcMathComp ();
Long lRes = cls. Add (23, 40 );
Cls. Extra = false;
The complete program code is as follows:
Namespace mcClient
{
Using System;
Using mcMath;
Public class Class1
{
Public Class1 ()
{
}
Public static int Main (string [] args)
{
McMathComp cls = new mcMathComp ();
Long lRes = cls. Add (23, 40 );
Cls. Extra = false;
Return 0;
}
}
}
So far, we have completed all the work of a component from production to testing.