How to use Visual C # to do components

zhaozj2021-02-16  45

Using Visual C # to do components, it is actually a very easy thing. At least more easily more than Visual C . In this article, the author will tell you how to make a component. We can complete this process by two steps: 1. Make a component; 2. Test this component at the client. 1. Make a component 1. First create a new class library project file to choose 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. The project file and the Solution Explorer in the project file he contained therein have 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. 3. Namespace McMath Double-click Class1.cs, you will see Namespace McMath, when we call components in the client, it will involve this namespace: public long, long Val2) {return 0;} public Bool Extra {get {}} To this, after confirming that the above is completed, you can find McMath.dll in the bin / debug directory when compiling this project file, which is the component. But at this time, the components do not have any functions, and we will further improve it. 4. Join a method Open ClassView from View Menu, we see the class1 does not have any methods and properties. Now let's add a method and a property. Class1, right click, select Add-> Add Method ... The method of making the Visual C # is popped up. With the help of this wizard, you can join the name, access type, return value, parameter, or even annotations of your component. Use the Add and REMOVE buttons to easily increase or delete parameters from the parameter list. We add a way here: long add (long Val1, long2), this method is to add two numbers and return them and. 5. Add a property repeatedly add to the process, in the same location, open the C # Attribute Wizard, add an attribute as shown below.

After the above process is completed, Class1 becomes the following appearance: Take a closer look at the class 1 class, you will find the wizard to join two functions in our components: public long add (long Val1, long Val2) {Return 0;} public bool extra {get {return true;} set {}} 6. To the Class Add code to transform the Class in accordance with the code below, and the Class1 is renamed McMathComp (this is because Class1 is the default name, which is easy to cause confusion. There may be 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 a DLL compilation engineering file generated 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. Newly built a control application Select File-> New-> Project-> Visual C # Projects-> Console Application. We will test our components through this control application. 2. Add a reference to 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 the McMathComp object, and call it to the following steps, you can easily call methods and properties.

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

New Post(0)