Using Visual C # as a component is an easy thing to do. At least it's much easier than using Visual C + +. In this article, I'll give you a detailed description of how to make a component. We can complete this process in two steps: 1. Make a component; 2. Test this component on the client.
I. Making a component
1. First, create a new class library project document
Select File->new->project->visual C # projects->class Library. Fill in the project file name and select the directory to store the file through the browse button. then click OK.
2. The engineering document and the documents he contained
The Solution Explorer in the project file now has two C # classes added. The difference is AssemblyInfo.cs and Class1.cs we just care about is Class1.cs can, AssemblyInfo.cs can ignore it.
3. Namespace McMath (namespace McMath)
If you double-click Class1.cs, you will see namespace McMath, which we will refer to when the client invokes the component:
Public long Add (long val1, long Val2)
{
return 0;
}
public bool Extra
{
Get
{
return true;
}
Set
{
}
}
At this point, after confirming that the above things have been done, compiling the project file can find the McMath.dll in the Bin/debug directory, which is the component. But this time the component does not have any function, below we will further refine it.
4. Add a method
Opening ClassView from View menu, we see Class1 without any methods and properties. Now let's add a method and a property to it.
Point in Class1, right click, select Add->add Method ...
The Visual C # Method Creation wizard window pops up. You can add a method name, access type, return value, parameter, or even comment to your component with the help of this wizard. The Add and Remove buttons make it easy to add or remove parameters from the argument list. We add a method here: Long Add (Long Val1,long val2), the function of this method is to add two numbers, and then return their sum.
5. Add a property
Repeat the process of adding the method, and in the same place, open the C # Property Wizard and add a property to the component as shown in the following figure.
After the process has been completed, the CLASS1 becomes the following:
Take a closer look at the Class1 class and you'll find that the wizard has added two functions to our component:
Public long Add (long val1, long Val2)
{
return 0;
}
public bool Extra
{
Get
{
return true;
}
Set
{
}
}
6. Add code for Class
Change class with the following code, and rename Class1 to Mcmathcomp (this is because CLASS1 is the default name, which can cause confusion and may be problematic when the client invokes the component).
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. Build DLL
Compile the project file generation component, which will be in the Bin\Debug directory of the engineering file, and the file name extension is the DLL.
Two Test DLL
On the client side, use Visual C # to invoke the component that we just built, and follow these steps.
1. Create a new control application
Select File->new->project->visual C # projects->console application. We test our components with this control application.
2. To add a reference to the namespace (namespace)
Open Project->add Reference, browse to the DLL you just generated, and then press OK.
The Add Reference Wizard will add the reference to the class of the current project file.
3. Call the McMath namespace, create the Mcmathcomp object, and call its methods and properties
Follow the steps below to make it easy to invoke methods and properties.
(1) using namespace, using McMath;
(2) Create Mcmathcomp object;
Mcmathcomp cls = new Mcmathcomp ();
(3) calling methods and attributes;
Mcmathcomp cls = new Mcmathcomp ();
Long lres = cls. Add (23, 40);
Cls. Extra = false;
Here's the Complete program code:
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;
}
}
}
At this point we have completed a component from production to testing all the work.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.