0

I created a new project (web project in C#). Created a new folder in my project called App_Code. I then proceeded to add a class with the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        }
    }
}

And in one of my pages default.aspx in the code behind I tried to do:

int i = BL.JustSomeTest();

I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context. But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.

In my default.aspx page I've tried to add a using statement

using Shipper.

But as soon as I do that my namespace BusinessLayer is not shown... Im confused?

Edit

Here is default.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;

namespace Shipper
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetDefaults();
                int i = BL.JustSomeTest();
            }
        }
   }
}

Here is my BL.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        } 
    }
}

The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?

oJM86o
  • 2,108
  • 8
  • 43
  • 71
  • 2
    Your method is defined outside of a class. – Dmytro Shevchenko Apr 20 '12 at 13:09
  • Can you update your answer with the calling code in the aspx? – mgnoonan Apr 20 '12 at 13:09
  • @Shedal that was a mistake on my part I edited the question again. – oJM86o Apr 20 '12 at 13:13
  • 1
    Have you tried to clear the solution and rebuild all? – Steve Apr 20 '12 at 13:20
  • @Steve what does it mean to clear the solution. I have done Build->Clean Solution, and Build->Build Solution. If I have a class in an app_code folder in a namespace do I need to add that as a reference? – oJM86o Apr 20 '12 at 13:22
  • Yes I mean that. No I think you don't need to add the namespace. Of course, if the BL.cs is not compiled in a separate project from your pages. Can you look at full path in the properties of BL.cs. Is saved in the App_Code path? – Steve Apr 20 '12 at 13:31
  • @Steve yes it is showing the correct path and I looked in windows and it shows exactly that path with the file in it. – oJM86o Apr 20 '12 at 13:35
  • This solved it...http://stackoverflow.com/questions/1222281/app-code-classes-not-accessable-asp-net I would think compile would be the default :(. – oJM86o Apr 20 '12 at 13:39

7 Answers7

5

your...

public static int JustSomeTest()
 {
   return 1;
 }

...is out of the 'class' - you cannot have methods defined for the namespace alone.

(at least from your example, it might be just a typo but then you'd need to give us a working example)

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • That was a mistake in typing it out in stackoverflow i have edited it. – oJM86o Apr 20 '12 at 13:11
  • try these links - http://stackoverflow.com/questions/1222281/app-code-classes-not-accessable-asp-net - http://stackoverflow.com/questions/155105/how-come-classes-in-subfolders-in-my-app-code-folder-are-not-being-found-correct - http://stackoverflow.com/questions/520429/organizing-classes-into-namespace-in-the-app-code-folder-not-working-as-expected – NSGaga-mostly-inactive Apr 20 '12 at 13:32
2

your method was outside a class. That's a no-no. Try this instead:

namespace Shipper.BusinessLayer
{
    public static class BL
    {
        public static int JustSomeTest()
        {
            return 1;
        }
    }
}

Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
  • When i build it says The type of namespace name BusinessLayer does not exist in the namespace shipper (are you missing an assembly reference)? – oJM86o Apr 20 '12 at 13:12
  • is this in another assembly? Have you referenced that assembly? – Jeremy Holovacs Apr 20 '12 at 13:15
  • See my edit Jeremy. This BL.cs file is in the same project, its all one solution. The BL.cs file is in a folder called App_Code. – oJM86o Apr 20 '12 at 13:18
  • And if you make the class not static? Does that make any difference? – Jeremy Holovacs Apr 20 '12 at 13:21
  • I keep saying if I do `using Shipper.BusinessLayer` the message I get back from vs is `The type or namespace BusinessLayer does not exist in the namespace Shipper (are you missing an assembly reference). – oJM86o Apr 20 '12 at 13:23
  • For giggles (this should not be relevant but I have seen these symptoms fixed by this before) can you check the profile of the project? Is it set to Client Profile? – Jeremy Holovacs Apr 20 '12 at 13:38
0

try

using Shipper.BusinessLayer;

Things to try

  1. Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
  2. In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Thats my issue it does not see BusinessLayer. – oJM86o Apr 20 '12 at 13:09
  • Verify the namespace on the other project via its project properties in visual studio. Verify they match what you think. If you have reference the project in the web project, browse to it in the object browser and verify you see the namespace. – ΩmegaMan Apr 20 '12 at 13:29
0

Try to delete the namespace declaration:

using System;

public static class BL
{
    public static int JustSomeTest()
    {
       return 1;
    }
}
npclaudiu
  • 2,401
  • 1
  • 18
  • 19
0

This forum thread on Wrox might be also useful.

npclaudiu
  • 2,401
  • 1
  • 18
  • 19
0

First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.

Then make sure the Shipper project is re-built, best bet to do a clean and build.

Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.

dice
  • 2,820
  • 1
  • 23
  • 34
0

Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)

Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!

Bex
  • 4,898
  • 11
  • 50
  • 87