Ever building a code, which you would like to have in every program you make? Most probably. So, if you are coding in C#, this is the way to do it – through libraries.
So, these are the steps:
- In Visual Studio – File>New Project>Class Library.
- Then open the class library and make the classes. Compile it. You need to make sure that a *.dll file is generated in the debug folder of the project.
- When you want to use the library in a new project, go to References in the Solution Explorer and add the library *.dll file through the browse button. Then everything works perfectly, even the intelli-sense.
- Here are some screenshots 🙂
And here comes the code, executed with a library.
The code:
1 2 3 4 5 6 7 8 9 10 11 |
class ExtTest { static void Main() { cls_worker w1 = new cls_worker("Vityata", 1000); cls_worker w2 = new cls_worker("Vitosh", 500); w1.Greet(); w2.Greet("evening"); } } |
The library:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; public class cls_worker { string _name; double _salary; public cls_worker(string name, double salary) { _name = name; _salary = salary; } public void Greet(string time_of_day = "morning") { string str_greeting_m = "Good morning, my name is {0}. My salary is {1}."; string str_greeting_e = "Good evening, my name is {0}. My salary is {1}."; if (time_of_day == "morning") { Console.WriteLine(str_greeting_m, Name, Salary); } else { Console.WriteLine(str_greeting_e, Name, Salary); } } public string Name { get { return _name; } } public double Salary { get { return _salary; } } } |
Enjoy it! 😀