I am having a problem with Visual Studio 2015 and Framework 4.6.1.
I have a business layer with this namespace: BusinessLayer.LocalStorage. In this class (named LocalStorage) I have this function:
...
public static void XXX()
{
}
...
In the main project with the same Framework 4.6.1, I have in one Winform
using BusinessLayer.LocalStorage;
In the Load I wrote the function but the reference is not recognized:
I have to add LocalStorage before:
LocalStorage.LocalStorage.XXX();
This way the call is working
why is not working using LocalStorage.XXX()? I declared my namespace in the using clausules.. Then should be working.
I have the class in a directory inside BusinessLayer project for this reason namespace should be BusinessLayer.LocalStorage
Code is:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
...
public static void XXX()
{
}
...
}
}
This is because LocalStorage is ambiguous, it could either be namespace LocalStorage or the class LocalStorage. You can solve this in 3 ways:
Remove the namespace LocalStorage entirely and move everything in it inside the BusinessLayer namespace
Rename the class LocalStorage to something else
Add using LocalStorage = BusinessLayer.LocalStorage.LocalStorage to the top of your file where you need to use the class LocalStorage
Edit: Option 4. If you desperately want to keep the directory structure as it is right now (with a LocalStorage directory), you can alternatively tell Visual Studio that the directory LocalStorage is not a namespace provider. You can do this by editing the properties of the folder in the Solution Explorer
Visual studio automatically generates Namespaces based on the current location of a class within a solution. When you create a new class it first takes the name of the project. Then it takes the current folder the class is located in. it will continue appending the folder name to the namespace until it reaches the class name itself.
So for example, if you have a project called ExampleProject, with a folder called Models, with a class inside called BaseModel.cs, the resulting namespace would be ExampleProject.Models
Make sure the namespace containing XXX is not LocalStorage.LocalStorage and is called BusinessLayer.LocalStorage. You can change this namespace to whatever you want and rebuild your project.
Although the namespace structure and object names are not recommended to be the same, which will lead to errors in many cases, I see that you want to use it this way from the comments you made.
I don't recommend you to name a class like its namespace, see this article
I would recommend would be <XYZ>LocalStorage.cs
I don't know your layer structure, but you can call it LocalStorage.XXX () with a structure like the one below.
BusinessLayer structure:
LocalStorage Class:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
public static void XXX()
{
}
}
}
And finally, after referencing BusinessLayer to winForm, you can call it in Load method as follows.
using System;
using BusinessLayer.LocalStorage;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LocalStorage.XXX();
}
}
}
Or, if you don't want a folder in your BusinessLayer structure, you can use it using the same namespace (namespace BusinessLayer.LocalStorage) as below.
Related
I have a web site(not web app) which has a default.aspx and default.aspx.cs.
And it has an App_Code folder with class A in it.
So default.aspx.cs. Looks like:
namespace test1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
And class A:
namespace test1
{
public class A
{
_Default mDefaultRef;
public pageLogicIndividual(_Default defaultRef)
{
mDefaultRef = defaultRef;
}
}
}
I can use A in _Default. eg. if I type test1., while working in class _Default, the IntelliSense will show both classes.
But if I type test1., while working in class A, the IntelliSense will only show A.
Why can't I use _Default in A?
Error : the type or namespace name does not exist in the namespace
(are you missing an assembly reference )
Edit:
I'll try to clarify.
While working in the .cs file where class _Default resides I can type test1., which is the namespace, and the intellisense will show me class _Default and Class A.
But if I'm working in the .cs file where class A resides and type test1., which is the namespace, then the intellisense will only show me class A.
I have had this challenge in the past.
Open up the App_Code folder node in Visual studio.
Right click on the concerned class, then click properties
In the properties pane, change Build Action to Compile.
It should work fine now.
Your problem is with your misleading namespace that you've added yourself after the new file has been created because ASP.NET Web Sites do not have namespace. The namespaces are available in Web Applications projects. i.e. after a new WebSite is created, namespace doesn't added to the files.
So you don't need to place your class A inside the test1 namespace because you can use A in default.aspx.cs even without namespace but you can not access other WebForm page classes from a Webform page or App_Code classes.
BTW if you want to use the necessary and reusable methods within a class of the Default Web Form, you can move those methods out to A class which is under App_Code and as I said already you can use it within all the Web Form CodeFiles without providing namespace for it.
In a nutshell, you cannot access page classes from App_code classes.
This restriction comes from website project compilation model. Special App_code folder contains shared classes (and possibly other resources) which are available to pages (and to each other). During compilation App_code is compiled first in a single assembly. This is the key point. Pages, controls and MasterPages are compiled in another assembly(ies) which may have references to the App_code assembly (but not vise versa). So this is one-way road.
No namespace declaration should circumvent this behavior.
Pages can see each other in ASP namespace (ASP.default_aspx) but pages usually don't have public properties / methods (user controls .ascx usually have).
Read better explanation on MSDN Code-Behind Pages
I'm using a public LoginContext class to manage user logins in my web app.
Unfortunately, even though I have the LoginContext class declared publicly, my partial class Login at Login.aspx.cs can't seem to access it.
My code is as follows:
// ~/App_Code/LoginContext.cs
namespace stman
{
public class LoginContext
{
}
}
// ~/Login.aspx.cs
namespace stman
{
public partial class Login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
LoginContext log = new LoginContext(); // error is here
}
}
}
The error that comes up on the line where I instantiate LoginContext reads as follows:
The type or namespace name 'LoginContext' could not be found (are you missing a using directive or an assembly reference?)
When I try to generate a new class for LoginContext, it goes into the web app's root folder where it can no longer access the public Database class that I need in LoginContext.
I have no idea what's causing all of these problems, but based on what I've learned over the last 18 months doing this professionally, they shouldn't exist right now...
Can anyone help clear things up here? Specifically I'd like to know:
What I'm doing wrong
Why it's wrong
Who can I fix it?
Thanks in advance!
EDIT
I've had a look and it seems neither the Database class in ~/App_Code/Database.cs or the LoginContext class in ~/App_Code/LoginContext.cs are accessible to the page - or any page in the website.
In LoginContext.cs properties, marked it as BuildAction = Compile.
You can achieve this behaviour when these classes are located in different projects.
If this is true then you should use full path to the class starting from the project name
ProjectName.stman.LoginContext
try using constructor
namespace stman
{
// Database is a class that handles the sql queries and such
public class LoginContext : Database
{
public LoginContext () : base("Name=LoginContext")
{
}
}
}
From what I read from the App_Code behavior may be different in web site projects and web application. I wonder what kind of project you're working on? One possible solution would be to make this project in web application project, this Link can help you to make this project:
Source
I know there are several similar topics like this on this site, but I can't find a solution that works. I have a solution called 'SportsStore' and it contains 3 projects, ALL that use the full .NET 4 framework. The projects are named 'SportsStore.Domain', 'SportsStore.UnitTests' and 'SportsStore.WebUI'.
Within the 'SportsStore.WebUI' project, I created a folder called 'Infrastructure' and in it I have a class called 'NinjectControllerFactory.cs' The complete code for it is below. Note the last 'using' statement at the top: 'using SportsStore.Domain.Abstract'. My program will NOT compile and it tells me the namespace does not exist. Intellisense recognizes 'SportsStore' but only says 'WebUI' is my next option. It will not recognize 'SportStore.Domain' at all. I have tried cleaning, rebuilding, closing, opening, rebooting, changing frameworks back to Client for all projects and then back to Full, and nothing seems to work.
Bottom line is I'm trying to get access to my IProductRepository.cs repository file which is part of the SportsStore.Domain.Abstract namespace in the 'SportsStore.Domain' project.
I'm hoping this is something easy to correct? Thanks in advance!
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System.Linq;
using Ninject;
using Moq;
using SportsStore.Domain.Abstract;
//To begin a project that uses Ninject and/or Moq (mocking data) you
//need to add reference to them. Easiest way is to select 'View', then
//'Other Windows', and 'Package Manager Console'. Enter the commands:
//Install-Package Ninject -Project SportsStore.WebUI
//Install-Package Ninject -Project SportsStore.UnitTests
//Install-Package Moq -Project SportsStore.UnitTests
//We placed this file in a new folder called 'Infrastructure' within the
//'SportsStore.WebUI' project. This is a standard way to define what we
//need to do with Ninject since we are going to use Ninject to create our
//MVC application controllers and handle the dependency injection (DI).
//To do this, we need to create a new class and make a configuration
//change.
//Finally we need to tell MVC that we want to use this class to create
//controller objects, which we do by adding a statement to the
//'Global.asax.cs' file in this 'SportsStore.WebUI' project.
namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
//During development, you may not want to hook your IProductRepository to
//live data yet, so here we can create a mock implementation of the data
//and bind it to the repository. This is MOQ in work - great tool to allow
//you to develop real code and make it think it's using the live data. This
//uses the 'System.Linq' namespace
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new List<Product>
{
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
}
}
}
In order for a class to access a type declared in another assembly, you must reference them. If you are using Ninject (I suppose), you have did this to the Ninject assembly.
Even though Domain is an assembly of yours, on the same solution, you must reference it at the WebUI, otherwise they won't know each other.
So, let's make sure:
Right click your WebUI project
Select Add reference
Go to Project tab
Select Domain project
Done!
Rebuild and you're good to go!
Regards
I have a web service with the namespace as the following:
namespace MyNS
{
class MyObject
{
//Implementation here
}
}
And I published the webservice and try to consume that webservice from the next C# Website.
I assigned the service name to "MyWS".
When I try to write the code
I have to write,
MyWS.MyObject obj = new MyWS.MyObject();
But I want to write
MyWS.MyNS.MyObject obj = new MyWS.MyNS.MyObject();
The problem is there might by MyObject class under other Namespaces. So, I want to identify my classes by NameSpaces.
What should I do to use Namespace in the coding?
I do not believe the original namespace is exposed as part of the WSDL, which is what the generation process uses to create the client end point.
This means there is no way to control what namespace your web service is used as.
However you can control what namespace your client end point uses. When you add the service reference, the bottom of the first page (Titled Add Service Reference, and containing Discovery controls) is a text box titled "Namespace". If you change that field to MyWS.MyNS when adding the service it should allow you to reference it as such.
The problem is there might by MyObject class under other Namespaces. So, I want to identify my classes by NameSpaces.
The compiler will give you an ambiguous reference error if you try to use conflicting object names. You will have to qualify them in that case.
So, if you want to give it a certain alias, just type the name as you want it.
namespace This.Is.My.Favorite.Namespace
{
public class MyObject()
{
//stuff
}
}
namespace MyNS.MyWS
{
class MyObject
{
//Implementation here
}
}
I have written a console application in C# /VS2008. In that I have multiple classes declared without specifying any accessibility modifier. Like
Namespace MyNamespace
{
Class MyClass
{
..
}
}
Now I added a new console application for testing purpose. I added reference to NUnit framework dll. And then a reference to my main project dll. But when I try to create an object of MyClass into my TestFixture class, then I get an error like "MyNamespace.MyClass is inaccessible due to its protection level"
Do I need to create my class as public? But what if my project cannot afford it?
The class needs to be public if you want it to be accessible from another assembly:
namespace MyNamespace
{
public class MyClass
{
}
}
If your project cannot afford it you may take a look at [InternalsVisibleTo] attribute.