Visual C# .NET: How to Create a Class Library

来源:百度文库 编辑:神马文学网 时间:2024/04/30 00:22:10
How to Create a Class Library in C#.NET
Computer Tutorials List
Beginners Computing
Microsoft Word
Microsoft Excel
Web Design
Cascading Style Sheets
Javascript
Visual Basic .NET
>Visual C# .NET
Beginners PHP
Qualifications and Certificates


 
You can create a class library of your code, save it as a DLLand thenreuse your C# code in other projects. The process is quitestraightforward.
Start a new project. From the New Project dialogue box,select ClassLibrary instead of Windows Application. In the Name box,type aname for your Class Library. Call it calculatorMethods,for thislesson. Then click OK.
When you click OK, C# will create a Namespace with the Nameyou'vejust used, and you'll be looking at the code window:

Note that the class is called Class1. You can change that toanythingyou like. But change it to calculator. Locate Class1.css inthe SolutionExplorer on the right:

Right click, and select Rename from the menu. Rename it to calculator.cs:

Press the Enter key on your keyboard, and you will then get amessagebox telling you that you are about to rename a file, and wouldyou liketo rename all references. Say Yes, and your code windowwillthen look like this:

Class1 has now been renamed to something useful - calculator.You cannow go ahead and add the code for your Class:

In the image above, we've added two simple methods, one thatadds upand one that subtracts. Note that they are both publicstaticmethods. This is only because static methods offer an easy wayto setup a code library, and not down to any sort of recommendedcoding techniquefor class libraries. They are for illustration purposes only!
Now that you have some code, click Build > BuildSolutionfrom the C# menu bars at the top. Click File > Save As,aswell. Now have a look at the Solution Explorer again. Clickthe Iconfor All Files, and then expand the bin > Debugfolder:

And there's our DLL file! We can use this in other projects.To testit out, click File > Close Solution. Now start a newWindowsApplication project.
Add a new button to your form, and double click to get at thecode.From the menu bars at the top of C#, click Project > AddReference.You should see a dialogue box appear with a few tabs on it.Click theBrowse tab. Browse to where you saved your ClassLibrary. Thenlocate the Bin > Debug folder from above. Your DLLwill bein there:

Click OK to add the reference to your DLL. Have a look at theSolutionExplorer on the right, and you should see it appear on thelist of references:

Now that you have a reference, you can use your code.
Add the following to your button code:

You should see your Namespace on the IntelliSense list. Pressthe Enterkey on your keyboard to add it to your code. Then type a dot.Your classname should appear:

Again, press the Enter key on your keyboard to add it to yourcode.Type another dot and you'll see the methods:

Finish the code, and you may have something like this:

Run your programme and it should work fine.
And there you have it - an easy way to create your own classlibrariesand reuse your C# code.
 
 
 
 
But that's it for this beginners course on Visual C# .NET. Wehopeyou enjoyed it, and wish you all the best for your futureprogrammingcareer. Good luck!
<--Drawing Text
<--Back tothe C# .NET ContentsPage
View all our Home StudyComputerCourses