CharlesCVictor CharlesCVictor -4 years ago 91
ASP.NET (C#) Question

How to access the Methods inside a class of a Project DLL in C#

I have been learning C# and was trying to create a particular Logic in a separate Solution.

In this scenario, I created a Project with a few classes and each class has particular methods for certain processes. I have created a DLL for this Project, let's call it "LogicLibrary.DLL"

Now, I have created another Project where I added a reference to the "LogicLibrary.DLL".

Now, my problem is that I am not sure how I should access the methods inside the classes of the DLL.

Suppose LogicLibrary.DLL has a class "Calculator.cs" which in turn has methods

Add(int a, int b)
,
Sub(int a, int b)
,
Mul(int a, int b)
and
Div(int a, int b)
.

In the new project, how can I access any of the methods mentioned above. Is there any particular way of creating an object for the classes inside the DLL?

Would love your help!...Thank You!

Answer Source

You have imported the library. Now you need to tell the compiler how to find the class you intend to use.

All libraries have a default namespace. You can see it by right-clicking on your project and selecting Properties.

Your class definition can also indicate a namespace. For instance:

namespace LogicLibrary
{
     //Class(es) here
}

So, if you import this library by adding a reference, your code to consume the class has to tell the compiler where to find it. You can do this in a few ways:

You can import the namespace so that it gets applied automatically by the compiler:

using LogicLibrary;

You can also fully qualify the name of the class you intend to use:

var calculator = new LogicLibrary.Calculator();

Either way works. Namespaces are nothing more than logical containers meant to help you organize the code to avoid collisions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download