In asp.net, before calling class or method from class library in
project, you must import dll file (the result of building class library)
into the project and add it to
references folder. So you can call any classes or methods from the class library to use in your webform. To do this, there are 3 steps:
- Import dll file of class library into project (into bin folder)
- Add the dll file from bin folder into references
- Import class library namespace wherever you want to call class or method of the class library
1. Import dll file of class library into project (into bin folder)
a. Before import dll file, you must build a class library first. To do this, go to
Solution Explorer panel, right click on the class library, and then choose
Build. See picture:
|
Figure 1: Build Class Library |
b. After building Class library, you’ll get a file *.dll (for eg: your
class library’s name is ClsLib1, so dll file’s name is ClsLib1.dll).
Let’s add it in to
bin folder in your project. If you can’t see
bin folder, to be able see
bin folder in the project , first click on your project in
Solution Explorer panel and then go to
Menu bar--> Click on
Project --> choose
Show All Files. See picture below :
|
Figure 2: Show all Files in the project |
c. Now you can see
bin folder and
obj folder in the project. So you need to add
bin folder in to the project. In
Solution Explorer panel, right click on
bin folder and then choose
Include In Project.
|
Figure 3: Include bin folder into the project |
d. Time to add dll file into
bin folder in the project. In
Solution Explorer panel, right click on
bin folder--> mouse over on
Add and then choose
Existing Item….
|
Figure 4: Add dll file into bin folder |
e. When
Add Existing Item dialog appear, find your dll file of
the class library in your project folder. For example: your file path is
C:\Visual Studio 2010\Projects\WebApplication2\ClsLib1\bin\Debug. Your
dll file of the class library have the same name as the class library
and have extension *.dll. Select the dll file and then click
Add button. See picture:
|
Figure 5: Select dll file |
2. Add the dll file into references
a. Go to Solution Explorer panel, under the project namespace --> right click on References and then choose Add References. See picture:
|
Figure 6: Add class library object into references |
b. The Add Reference Dialog appear. In Browse Tab, find the dll file of your class library, which you added in bin folder in the project. For example: C:\Visual Studio2010\Projects\WebApplication2\WebApplication2\bin. Select dll file in the bin folder in project and then click OK. See picture below:
|
Figure 7: Browse dll file add into references |
3. Import class library namespace wherever you want to call class or method of class library
a. Import class library namespace
Syntax: Using Class_Library_Name;
Example: Your class library namespace is ClsLib1, so code: Using ClsLib1;
b. Calling class from class library in the project
Syntax: Class_Name variable_name = new Class_Name();
Example: Class2 cls2= new Class2();
|
Figure 8: Import namespace of class library and declare class in project |
Done ...Now you can calling all classes and methods of class library to use in the project :)
No comments: