简体   繁体   中英

Create and Using DLL in same Project in c#

I am having a DLL file. With the use of DLL, I have to call the methods and add some more methods in my project. Now, I need to migrate the older DLL to Make that project as a new DLL. I done this But the problem is The C# code is converted to net module it shows two errors. I am not clear about that. kindly help me over it.

DLL Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mcMath
{
public class mcMathComp
{
    private bool bTest = false;

    public mcMathComp()
    {
        // TODO: Add constructor logic here
    }

    /// <summary>
    /// //This is a test method
    /// </summary>
    public void mcTestMethod()
    { }

    public long Add(long val1, long val2)
    {
        return val1 - val2;
    }

    /// <summary>
    /// //This is a test property
    /// </summary>
    public bool Extra
    {
        get
        {
            return bTest;
        }
        set
        {
            bTest = Extra;
        }
    }
}

}

CS Project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mcMath;

namespace mcClient
 {
        class Program
        {

            static void Main(string[] args)
            {
                mcMathComp cls = new mcMathComp();
                long lRes = cls.Add(23, 40);
                cls.Extra = false;
                Console.WriteLine(lRes.ToString());
                Console.ReadKey();

            }
        }
    }

Errors:

Program.cs(5,7): error CS0246: The type or namespace name 'mcMath' could >not be found (are you missing a using directive or an assembly reference?)

Tried Methods:

  1. I will add the reference via Project-> Add Reference.
  2. The using Reference also used.
  3. Put the DLL into the current project debug/release folder

I'm guessing you used to have the code side by side, ie

public int Add(int a, int b)
{
    return a + b;
}
public void SomeMethod()
{
    var result = Add(2,3);
}

This works because the scope ( this. ) is applied implicitly, and takes you to the Add method on the current instance. However, if you move the method out, the scope is no longer implicit.

You will need one of:

  • the type name if it is a static method
    • or a static using if using C# 6
  • a reference to the instance if it is an instance method

Then you would use one of (respectively):

  • var result = YourType.Add(2,3); (plus using YourNamespace; at the top)
    • using static YourNamespace.YourType; at the top
  • var result = someObj.Add(2,3);

Checking the compiler message, it sounds like you've done something like (line 7):

using YourNamespace.YourType.Add;

which is simply wrong; you don't use using to bring methods into scope - only namespaces and (in C# 6) types.

Likewise, I suspect you have (line 22):

var result = YourNamespace.YourType.Add(x,y);

which is not valid as this is not a static method.

Create and Using DLL in same Project in c#

DLL or Class Library is a separate project that can be part of same solution.

As you already know, adding a reference to that dll/project will make it available in your app project. However if function Add in dll is in different namespace (which would be normal) u would need to add using clause at the beginning of your class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM