Less specific using statements conflicting with more specific ones - c#

Can anyone explain this behavior and what the solution is?
I installed Ninject.MVC3 via nuget, this creates a file in the App_Start folder called NinjectWebCommon.cs with the namespace like this:
namespace MvcApplication1.App_Start {
...
using Ninject;
using Ninject.Web.Common;
...
}
Now, I want to create an NinjectModule and am having some issues with the Ninject namespace being recognized.
namespace MvcApplication1.Ninject.Modules {
using Ninject.Modules
...
}
As soon as I add the using statement in the module, NinjectWebCommon.cs can no longer compile. If I place the using outside the namespace, it still won't compile.
If, however, i change the namespace for my module to MvcApplication1.Foo, then it works fine either way.
Why would naming this MvcApplication1.Ninject.Modules (or even just MvcApplication1.Ninject) cause NinjectWebCommon.cs to no longer find it's references? I thought the purpose of namespaces was to prevent this sort of thing?

Using statements within a namespace search in the child namespace of the current namespace and all its ancestors before looking at the global name space. E.g. If you have a namespace MvcApplication1.A you can write
using A
Instead of
using MvcApplication1.A
Because of this, your example is interpreted by the compiler as
namespace MvcApplication1.Ninject.Modules {
using MvcApplication1.Ninject.Modules
    ...
}
You can force that the compiler looks only in the global namespace like this:
namespace MvcApplication1.Ninject.Modules {
using global::Ninject.Modules
    ...
}

Related

Rider: set namespace autofill as absolute namespace instead relative

When I'm using new class the Rider import the file by using the relative namespace, there is an option to set the Rider to use the absolute namespace instead?
for example, instead
using Collections.Generic;
autofill
using System.Collections.Generic;
If I understand you correctly - you need to check this official page.
In project settings you can set root namespace for all your project, and all your classes will import this namespace by pattern like:
{root namespace}.{local namespace (such as Contracts)}.{subnamespaces and etc.}
For example:
If you have Project called as Castle
and interface in local namespace IContract, and you want to use Logger namespace you will see smth like this
using Castle.Logger;
namespace Castle.Contracts;
public interface IContract {}

System.Console resolving to incorrect namespace

I have a simple console app with the namespace of
something.service.console
The problem is when I try to use
Console.WriteLine("something");
I get compile error: The Type or namespace name "WriteLine" dos not exist in the namespace something.service.console.
So unless I use
System.Console.WriteLine("something");
The C# compiler is trying to resolve the method WriteLine to the incorrect namespace ("something.service.console").
In this scenario is it possible to force the compiler to resolve the Console.WriteLine to the correct namespace "System" (instead of renaming my namespace :))?
Thank you.
The compiler will find the namespace something.service before it finds the namespace System so it will assume that
Console.WriteLine("something");
actually means
something.serviceConsole.WriteLine("something");
and hence your error.
Two possible solutions are to either fully qualify the namespace when you have issues like this:
System.Console.WriteLine("something");
or change the name of your namespace so it's not something.service.console but something.service.somethinglese.
You could "force" it like this:
using SysConsole = System.Console;
Now whenever you use Console in is refering to System.Console
public class Console
{
private void Test()
{
SysConsole.WriteLine("something");
}
}
Note: There really is nothing bad about using:
System.Console.WriteLine()
and you should avoid using classnames that already exist in the .NET Framework.
Using a C# 6 feature "using static" you can change the code to in order to avoid the ambiguous name Console without cluttering the code.
This would make sense, if a lot of System.Console.WriteLine calls occur in the code.
using static System.Console;
namespace SomeNamespace.Console
{
public class SomeClass
{
public void SomeMethod()
{
WriteLine("abc");
}
}
}

The type or namespace name does not exist in the namespace (are you missing an assembly reference?)

I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name"?

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name" ?!
As, I'm working on a website, which uses SAP .NET connector. I already added a reference for connector ddl, and while referencing its namespace "using namespace_name", or set class namespace to another one rather than connector namespace,
I got error regarding connector classes with that error message "The type or namespace couldn't be found, are you missing a using directive or an assembly reference?".
But while changing namespace name to connector namespace, everything is going well?!
// Set namespace to be IDestinationConfiguration interface namespace.
// Using this, everything is going well.
namespace SAP.Middleware.Connector
{
public class ConnectorConfiguration : IDestinationConfiguration
{
}
}
// Using that way; it's not working, and got an error regarding IDestinationConfiguration even it belongs to refernced namespace.
using SAP.Middleware.Connector;
public class ConnectorConfiguration : IDestinationConfiguration
{
}
So, connector types forced me to set namespace of class to their namespace!
Is this possible? If so, how?
I ran into this too and couldn't figure it out until I finally found the answer on a post at http://forums.sdn.sap.com/thread.jspa?threadID=1876430, the last line which said:
"Also, make sure your target framework is ".NET v4.0" NOT ".NET v4.0 Client" -- that way you get the System.Web namespace that is required."
My project was targeting 4.0 client target framework. Once I changed it to just .NET Freamework 4.0, all the references worked as expected.
Is this what you are after?
public class ConnectorConfiguration: SAP.Middleware.Connection.IDestinationConfiguration
{
}
You can write all your code without usings if you like, you just need to use the fully qualified namespace name for every class/interface where the using isn't used.
If you try this:
using SAPTEST = SAP.Middleware.Connection;
namespace TestNamespace
{
public class ConnectorConfiguration: SAPTEST.IDestinationConfiguration
{
}
}
If that works, but it doesn't work if you remove SAPTEST, then IDestinationConfiguration must be declared in another namespace too.

C#.NET Namespace name does not exist in namespace error - only when using is outside local namespace directive - why?

Using .NET 2.0, C#, Windows Forms development, Enterprise Library 3.1.
We have a project namespace (call it Project). We also have several sub-namespaces inside of that project, for example Project.Namespace1, Project.Namespace2, etc.
In one class, we define enums and such to be used with the Enterprise Library Logging block, like this:
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
// enum values here
}
}
}
In another class, I use the enum values so I need to declare a using statement. Same project, so there is no assembly to reference, right?
If I declare the using inside of the local namespace, like this, it works fine:
namespace Project.SomeName
{
using Project.Logging;
// code referencing the Logging enum
}
However, if I put the using statement outside of the local namespace declaration, I get the "type or namespace name 'LogPriority' does not exist in the namespace 'Project.Logging'... Like this:
using Project.Logging;
namespace Project.SomeName
{
// code referencing the Logging.LogPriority.whatever
}
Why is this? Has anyone run across this before?
I have run into similar (though not exactly the same) problems before when using a class that has the same name as its namespace.
Oddly enough it seemed to compile ok on some developers pc's but not on others. In the end we made sure that no namespace contained a class of the same name.
namespace Project.Logging
{
public static class Logging // this is what caused the probems for me
{
}
}
I also had a wired error. I cannot find any namespace which is coming from different assemblies, but begins with executing assembly name.
Finally, I found out that I have set the target framework to .NET framework client profile.
Yes, most likely you have an unusual value set for the "Default Namespace" in your project properties. I would validate the project configuration.
We ran into this issue before and it all went down to ambiguous naming of the namespace and the class name.
When we tried to have our namespace as Services.Web.xxx and also add in a service reference as Services.Web.xxxx and ALSO add a references to an assembly that was named Services.Web.xxx you can only imagine the problems we ran into.
In the end to fix it we simply did a rename to make sure that there was only one instance of the Services prefix
Also you could do the following and create an alias to LogPriority to LogEnum:
using LogEnum= Project.Logging.Logging.LogPriority;
namespace Project.SomeName
{
internal class MyClass
{
public MyClass()
{
LogEnum enum1 = LogEnum.None;
}
}
}
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
None,
Default
}
}
}
It definitely can make a difference if you have usings inside or outside the namespace. There is a good discussion here, and it is likely to be related to your default namespace settings.

Categories

Resources