0

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
Ahamed Nafeel
  • 49
  • 3
  • 13
  • Does the reference to your DLL that contains the "Add" method show up in the references list in your project? Does it have a yellow triangle with an exclamation mark next to it? – irreal May 05 '15 at 06:57
  • 1
    Can you show the code that is *calling* `Add` ? You can't just use `Add` by itself (unless you are using C# 6 static usings, and a static method) – Marc Gravell May 05 '15 at 07:03
  • #Soner Gonul, Yes Dude, The method can be viewed. The Project may clean and Build Without errors. But I made the CS file Into a Net module It shows the above error. – Ahamed Nafeel May 05 '15 at 07:04
  • @AhamedNafeel again: can you show the code that is *calling* `Add`... – Marc Gravell May 05 '15 at 07:11
  • If the compiler does not complain on the new mcMathComp(); line, then the class you are referencing doesn't really contain a Public int Add function you mention in your question. Maybe show the dll file as well, at least the Add function declaration. – irreal May 05 '15 at 07:24
  • 1
    @irreal that isn't the message the compiler would emit even if that *were* the case; I don't think the code that is failing is where the OP thinks it is... – Marc Gravell May 05 '15 at 07:25
  • @MarcGravell you are absolutely right, i zoned out on the actual compiler error it was giving them. you are right, there's no way the OP is calling it like that and getting that error message :) sorry! – irreal May 05 '15 at 07:26
  • @AhamedNafeel please update the error codes your compiler is giving you as well. The error codes still present in your question are clearly not caused by the code you have shown us – irreal May 05 '15 at 07:50
  • everything is given now .. – Ahamed Nafeel May 05 '15 at 07:52
  • 1
    This just brings us back to the reference being wrong. You clearly do not have a valid reference to the "Dll code" project. Have you compiled the library? Double check that there is a reference to it in the "CS Project" Add it from the "Solution" tab in the add reference dialog. Beware that you have to check the checkbox next to the name, not just select that item in the list and click ok. Also double check that the referenced file actually shows up in your solution explorer under "References" in the "CS Project" – irreal May 05 '15 at 07:57
  • @Ahamed now that you've actually provided code: it is trivially obvious that you simply don't have a reference from the exe to the DLL; irreal has described how to fix that. If you had provided relevant info from the start, a lot of everyone's time would have been saved. – Marc Gravell May 05 '15 at 08:00
  • Also: don't use phrases like "net module" unless you know **exactly** what you mean - it sets an incorrect level of understanding; what you have created is a "class library" or a "DLL assembly" – Marc Gravell May 05 '15 at 08:02
  • sorry @Marc and Thanks... Class Library. – Ahamed Nafeel May 05 '15 at 08:05
  • @irreal Everything is well set and code runs. the problem is I may not convert the CS file into a net module. Kindly tell how to create a DLL , that can be included with other DLL. – Ahamed Nafeel May 05 '15 at 08:08
  • I must admit I'm not too sure what you mean by converting to a net module. what you need is a class library. Check your steps against a tutorial such as this one: http://www.c-sharpcorner.com/uploadfile/61b832/creating-class-library-in-visual-C-Sharp/ – irreal May 05 '15 at 08:12
  • @irreal the DLL created successfully. Just imagine this scenario. I create a DLL which contain only add method. Now I need sub,mul,and Div methods. So I create a New DLL with the 3 methods Instead of adding the same add code , I will add the ADD DLL into my project and access them it make a whole complete library.. Could You understand what I am trying to say?? – Ahamed Nafeel May 05 '15 at 08:20
  • No, not really. The question of how many dlls you will create, which methods you will put in which, etc, is not relevant to this discussion. We are trying to figure out why you can't reference a namespace from a class library project included in your solution. If you have succesfully followed the steps as outlined in the tutorial, there is no way you can get the error that you indicate having in your question. – irreal May 05 '15 at 08:23
  • From start , I said The DLL are created successful and They can refered correctly but you guys are not understand what I am trying to say.. – Ahamed Nafeel May 05 '15 at 08:29

2 Answers2

2

I'm guessing you used to have the code side by side, i.e.

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Dude, The code is working fine. But if I create the C# file into a net module it shows the error. I use some test class to run the methods it works fine. – Ahamed Nafeel May 05 '15 at 07:13
  • @AhamedNafeel if it doesn't compile, the code is not working fine. The important thing here is, and I stress that this is the third time I've asked: ***can you show the code that is calling `Add`*** (and ideally, how it relates to `Add` - i.e. is it in a different type?) You'll find it in Basic.cs, lines 7 and 22 – Marc Gravell May 05 '15 at 07:15
  • using AddDll; static void Main(string[] args) { mcMathComp cls = new mcMathComp(); long lRes = cls.Add(23, 40); cls.Extra = false; Console.WriteLine(lRes.ToString()); Console.ReadKey(); } – Ahamed Nafeel May 05 '15 at 07:20
  • @AhamedNafeel see my comment on your main post (also: you should really *edit that into the question*; I have done that for you) – Marc Gravell May 05 '15 at 07:24
  • I mention the Line editing my Main Code.. The problem occured only in line 7. 22nd issue Can be resolved it is an syntax error. – Ahamed Nafeel May 05 '15 at 07:34
  • @AhamedNafeel `using AddDll`? Is that the *namespace* the class is in? If you just copied the cs file over, it will still have the original namespace, even though it's in a different DLL. What's the full name of the `mcMathComp` type, including the namespace? – Luaan May 05 '15 at 07:36
0

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

Mladen Oršolić
  • 1,352
  • 3
  • 23
  • 43
  • The op mentiones "The using Reference also used." I hope that means they included the using statement. – irreal May 05 '15 at 07:08
  • @Mladen Oršolić , Thanks and sorry dude.. I call the DLL by the use of Using clause only. I can add the references and also I tried to put into the current bin/debug folder but it gives the same error. – Ahamed Nafeel May 05 '15 at 07:09
  • @AhamedNafeel adding a `using` directive by itself will not make it magically work (although `using static {Type}` might, if it is a static method); at the moment it is an instance method, so you need an *target instance*; if it was static, you would need to tell it which type to use (again, unless `static using` in C# 6). – Marc Gravell May 05 '15 at 07:11
  • static void Main(string[] args) { mcMathComp cls = new mcMathComp(); long lRes = cls.Add(23, 40); cls.Extra = false; Console.WriteLine(lRes.ToString()); Console.ReadKey(); } – Ahamed Nafeel May 05 '15 at 07:18
  • @AhamedNafeel are you *sure* that is the call from Basic.cs lines 7 and 22 ? Because that compiler message is **not** consistent with that code. Please check Basic.cs lines 7 and 22 – Marc Gravell May 05 '15 at 07:20
  • 7th line is the DLL using line. – Ahamed Nafeel May 05 '15 at 07:25
  • 22nd line is object created from the DLL line. – Ahamed Nafeel May 05 '15 at 07:25
  • 1
    @AhamedNafeel can you **show** line 7 and 22 please? It sounds like you've made a mess of your `using` directive; it shouldn't end with `.Add`, if that is what you have done... – Marc Gravell May 05 '15 at 07:26
  • Marc I mention the Line editing my Main Code.. The problem occured only in line 7. 22nd issue Can be resolved it is an syntax error. – Ahamed Nafeel May 05 '15 at 07:32
  • @Ahamed and yet the line you've added to your question mentions mcMath, not Add. You are making this impossible to answer, by not actually showing the relevant code. Please show lines 7 and 22 from Basic.cs, **as they actually are**. – Marc Gravell May 05 '15 at 07:49