3

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.

Umair Anwaar
  • 1,130
  • 9
  • 27
  • @TheGeneral its working fine when i go into menu `Build > Build Page` and again build solution everything working fine – Umair Anwaar Oct 26 '18 at 07:22
  • 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. – Umair Anwaar Oct 26 '18 at 07:24

2 Answers2

3

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.

Isma
  • 14,604
  • 5
  • 37
  • 51
  • I know this approach problem is I cannot create namespace cause of some application admin restriction. In whole application there is 100 of classes without namespace and working fine except this class and its also work when I build page individually if error occured. – Umair Anwaar Nov 02 '18 at 06:58
  • Its not allowed to me to change name of class cause its using in multiple application, services and integrated with 15+ systems. So question is why I am able build using Build Page option if compiler not build by build solution. – Umair Anwaar Nov 02 '18 at 12:06
  • I don't understand your question... sorry... but I think you should speak with your team about all the weird restrictions you have within your projects that is preventing you to compile it. – Isma Nov 02 '18 at 12:09
  • sorry for late. I ask to change namespace or Class name before i upload this question and project Leads stop me to make these changes. They says its working without change class name or adding new namespace if you get an error while compiling code then just go into menu and build that page individually. and currently I am doing this. But its not a right choice cause first you build and wait for 3 mins and error comes then build page and again build project it means you need at least 15 mins to run the project. – Umair Anwaar Nov 05 '18 at 08:10
  • Actually problem is I am not working on report. Its build by another developer. I am working on another side of application and required to build project and not allowed to modify existing developed working functions or class like Report i already suggest them to change name of class or put a namespace. just wanna know about why build page work and build solution first fails sometime not fails but many time fails. – Umair Anwaar Nov 07 '18 at 09:12
  • Yes, I understood your problem, that's why I proposed you to create a new report class and inherit from the one you can't change. If you can't do that either then I am no sure what you can do. – Isma Nov 07 '18 at 09:23
2

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.

Matt Shepherd
  • 782
  • 10
  • 21
  • Without knowing more about the structure of your solution it is difficult to offer more advice about how to debug this. Could you edit your question to include screenshots of the build order and the failed builds? – Matt Shepherd Nov 07 '18 at 02:24
  • Added Build Order screenshot – Umair Anwaar Nov 07 '18 at 09:27