[Turn] A simple example of making a component with Visual C #

xiaoxiao2021-03-06  99

1. Make a component 1. First build a new class library project file

Select File-> New-> Project-> Visual C # Projects-> Class Library. Fill in the name of the project file and select the directory to be stored through the Browse button. Then click OK.

2. Engineering file and the documents he contain

Solution Explorer in the project file at this time has added two C # class. They are AssemblyInfo.cs and class1.cs. We can care about Class1.cs, asmblyingInfo.cs can no matter whether it is. Rename Class1 is McMathComp. And add the following:

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;

}

}

}

}

3. Generate DLL

Compile the engineering file generation component, which will be in the bin / debug directory of the project file, the file extension is a DLL.

two. Test the DLL at the client, use Visual C # to call the components we generated just now, follow these steps.

1. Create a new control application

Select File-> New-> Project-> Visual C # Projects-> Console Application. We will test our components through this control application.

2. Add a reference for NameSpace (Name Space)

Open Project-> Add Reference, browse to the DLL you just generated, then press OK.

Adding a reference wizard will add the reference to the class of the current project file.

3. Call the mcmath namespace, create a McMathComp object, and call it method and properties

Follow the steps below to easily call methods and properties.

(1) Use Namespace to use mcmath;

(2) Create a mcMathComp object;

McMathComp CLS = New mcmathcomp ();

(3) Call method and attribute;

McMathComp CLS = new mcmathcomp (); long lres = cls.add (23, 40); cls.extra = false;

Here is a 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;

System.console.writeline (LRES.TOSTRING ());

System.Threading.Thread.Sleep (5000);

Return 0;

}

}

}

To this end, we have completed a component from making all the work to test. Run as follows:

转载请注明原文地址:https://www.9cbs.com/read-101186.html

New Post(0)