Build Solution Error class does not contain a definition for xxx when definition exist and Build Page work

Viewed 1690

I have a class with the name of 'Report' declared like this without specific namespace its declared as global namespace.

using system;
//and other namespaces

public class Report
{
    public string CheckIfReportAccess(string name)
    {
        //logic here
    }
}

and I am using Report Class method like this in webform pageload method.

global::Report report = new global::Report();
if (!report.CheckIfReportAccess("xxx"))
{
    //logic here.
}

Issue is when I built solution an error occurred

ERROR: 'Report' does not contain a definition for 'CheckIfReportAccess' and no extension method 'CheckIfReportAccess' accepting a first argument of type 'Report' could be found (are you missing a using directive or an assembly reference?).

What I am doing now i just go into menu Build > Build Page and rebuild solution. solution build and works. but its a very big problem its time consuming there is hundred of reference of this class and every time i build solution its appear and then build page rebuild solution works.

I am using Visual Studio 2017.

Edit Here is Build Order screenshot enter image description here

I also checked build order dependencies.

2 Answers

Instead of using the global namespace, you should assign a namespace to your class, for example if your application is called "MyApplication" you could create a folder call "Reports" and then create your class there as follows:

namespace MyApplication.Reports
{
    public class Report
    {
        public string CheckIfReportAccess(string name)
        {
            //logic here
        }
    }    
}

If you want to use it in your Page's code behind, you should be able to do so simply referencing the namespace as you are doing now:

using MyApplication.Reports;
[...]
protected void Page_Load(object sender, EventArgs e)
{
    Report report = new Report();
    report.CheckIfReportAccess("SomeReport");
}

If you want to make this class available to your views you have two options:

  1. Declare it in your Web.config using the namespace:

    <pages> <namespaces> <add namespace="MyApplication.Reports"/> </namespaces> </pages>

Then you can access all classes within that namespace in your views directly:

<% Report report = new Report(); %>

<% if(report.CheckIfReportAccess("SomeReport") == "1") { %>
    <div>
        SomeReport has access
    </div>
<% } %>
  1. Import it directly in your View:

<%@ Import Namespace="MyApplication.Reports" %>

Edit

If you can't apply namespaces as described above and need to use the global namespace for all classes, you may consider changing the class name if you have problems with it, for example, instead of Report try with MyReport and see if that resolves the problem.

Edit 2

If you cannot modify the existing class due to project restrictions (which I find weird anyway) you can always create a new class with the namespace and name you want and inherit from the problematic class:

namespace MyApplication.Reports
{
    public class MyReport : Report
    {
    }    
}

Then use "MyReport" in your View and do not forget to register the namespace in your Web.Config as described above.

From your comments on the question and on @Isma's detailed reply to your question, it sounds as though you might have an issue with your project build order. Specifically:

first build solution then error comes on error page Build page and then build solution again. its my work around now but its very painful.

When you Build a solution, the files are output to the appropriate bin folder for your build configuration. If this fails once, and then succeeds the second time, it suggests to me that the first build is failing because the assembly containing your Report class has not been created by the time the project referencing the Report class is built. The second time you run the build, the assembly has been created (at some point in the first build, but after it was needed), and so the problem no longer appears.

You can view and change the order that your projects are built in by following the instructions in the answer to this question: Visual Studio 2010: How to enforce build order of projects in a solution?

You will need to ensure that the assembly containing the Report class is listed earlier in the build than all assemblies containing references to that class.

Related