|
You can create a class library of your code, save it as a DLL and then
reuse your C# code in other projects. The process is quite straightforward.
Start a new project. From the New Project dialogue box, select Class Library instead of Windows Application. In
the Name box, type a name for your Class Library. Call itcalculatorMethods,
for this lesson. Then click OK.
When you click OK, C# will create a Namespace with the Name you've just
used, and you'll be looking at the code window:
Note that the class is called Class1. You can change that to anything you
like. But change it to calculator. Locate Class1.css in the Solution Explorer
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 a message box
telling you that you are about to rename a file, and would you like to rename
all references. SayYes, and your code window will then look like this:
Class1 has now been renamed to something useful - calculator. You can now
go ahead and add the code for your Class:
In the image above, we've added two simple methods, one that adds up and
one that subtracts. Note that they are both public
static methods. This is
only because static methods offer an easy way to set up a code library, and
not down to any sort of recommended coding technique for class libraries.
They are for illustration purposes only!
Now that you have some code, click Build
> Build Solution from
the C# menu bars at the top. Click File
> Save All, to save all your project files. Now have a look at the
Solution Explorer again. Click the Icon for All
Files, and then expand the bin
> Debugfolder (Look in the bin
> Release folder if a bin > Debug folder is not created):
And there's our DLL file! We can use this in other projects. To test it
out, click File > Close
Solution. Now start a new Windows Application project.
Add a new button to your form, and double click to get at the code. From
the menu bars at the top of C#, click Project
> Add Reference. You should see a dialogue box appear with a few tabs
on it. Click the Browse tab. Browse to where you saved your
Class Library. Then locate the bin
> Debug folder from
above. Your DLL will be in there (check the bin
> Release folder if it's
not):
Click OK to add the reference to your DLL. Have a look at the Solution
Explorer on the right, and you should see it appear on the list 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. Press the Enter
key on your keyboard to add it to your code. Then type a dot. Your class name
should appear:
Again, press the Enter key on your keyboard to add it to your code. 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 class libraries and
reuse your C# code.
|
No comments:
Post a Comment