Keyword global:: when adding reference - c#

I've encountered a strange phenomenon today as I added a reference of a project to another project and trying to use an interface of the referenced assembly:
Situation:
The class MySampleApplication is in the project MyApplication.
The interface IMySampleApplication is in the referenced project MyApplication.Interfaces.
When I do this:
public class MySampleApplication : IMySampleApplication
Resharper adds the following using:
using global::MyApplication.Interfaces;
Question:
Why is it adding the global:: prefix and how can I avoid this?
Thanks in advance

You'll have two occurrences of MyApplication somewhere in your code, eg you'll likely have a class called MyApplication in your local project as well as the namespace in the MyApplication project.
The local MyApplication will take precedence over the namespace in your MyApplication project, so MyApplication.Interfaces will not be found. To get around this, the special alias global:: can be used to tell the compiler that in this case MyApplication refers to a base namespace and so MyApplication.Interfaces can then be correctly identified.
As to how to avoid it, that's easy to say, but it may be harder to achieve: rename one of them to remove the name conflict...

You probably have another entity called MyApplication in your project which is hiding the namespace.
See: "How to: Use the Global Namespace Alias (C# Programming Guide)"

Related

Type or namespace name 'xxx' could not be found in Umbraco

When I created a class, there is no existing namespace declaration and I need the namespace declaration to access the class from a view (cshtml).
Normally when you create a class, there should be a namespace declaration included right? But for me I don't see any.
I already tried to add a namespace declaration, but I still can't access it from another class.
I hope you could help me with this.
Screenshots: https://drive.google.com/open?id=0B5TGcf9mTeg2V2ZDaldWelp6WXc
ScreenShot
make sure your namespace name is correct .using PRL(Capitalized) .Models .And the good practice is add an empty web application project ,not a website.
Just as what Nico Said (https://stackoverflow.com/users/5685258/nico)
Follow this instructions and this will be resolved:
https://our.umbraco.org/documentation/Getting-Started/Setup/Install/install-umbraco-with-nuget
Although I started all over again.
~I created a new project from Visual Studio 2017 using ASP.NET Web Application
~Selected an empty file
~ Right click on the project
~ Click Manage NuGet Packages and installed Umbraco CMS
~ Run it from VS and install the umbraco platform using the browser
~ As soon as I create a class, a namespace declaration will also be included.
Thanks!
Your model belongs to namespace PRL.Models but you try using Prl.Models - namespaces are case-sensitive. Try using PRL.Models
Edit: and for the class without namespace-declaration: you should always have a class in a namespace. Declare one.

Warning CS0436 suddenly appears when upgrading to VS2015

I've just upgraded from VS2013 to VS2015, and a ton of CS0436 warnings have appeared, all seemingly relating to the same issue.
I am slowly migrating web applications from VB to C#, so perhaps this is something really simple. I'm new to C# so please use layman-type answers...
My solution is structured as such:
Project 1 - Reusable methods (database access, etc)
\CommonDataAccessFunctionality.vb
Namespace MyCompany
Public Class CommonDataAccessFunctionality
Public Sub New(ByVal storedProcedureToRun As String)
' db stuff here '
End Sub
End Class
End Namespace
Project 2 - Web Applicable (C#) with dependency on Project 1
App_Code\DataAccess.cs
using System.Data;
using System.Data.SqlClient;
namespace QrCodes.App_Data
{
public abstract class QrDataCommon : MyCompany.CommonDataAccessFunctionality
{
public QrDataCommon(string storedProcedureToRun)
: base(storedProcedureToRun)
{
}
}
public class QrDataGrabber : QrDataCommon
{
public QrDataGrabber(string storedProcedureToRun)
: base(storedProcedureToRun)
{
}
}
}
The error is shown on this line:
public class QrDataGrabber : QrDataCommon
Warning CS0436
The type 'QrDataCommon' in 'D:\Web\wwwroot\MyApp\MyApp-InProgress-Person
WebApi\QrCodes\App_Code\DataAccess.cs' conflicts with the imported
type 'QrDataCommon' in 'QrCodes, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. Using the type defined in
'D:\Web\wwwroot\MyApp\MyApp-InProgress-Person
WebApi\QrCodes\App_Code\DataAccess.cs'.
I've read many posts on here about a project referencing itself, however, there are no references in the project dialog (that are listed), only a single dependency in Project 2 to Project 1. Also, Project 2 has no controls, etc, that reference anything else.
Can anyone please give me some guidance? I'm not sure if its helpful, but in the object browser when searching for 'QrDataCommon' I see this:
Update based on help so far
If I opt to view all files within Solution Explorer, within the \bin\ directory there is a file called QrCodes.dll that gets created when the project/solution is built. I also see the same in the hidden Debug folder.
If I rename the QrDataCommon class to something totally unique, say QrDataCommonTest123, and clean/rebuild, the error immediately updates to use the new class name.
Therefore, might this be something to do with the application build target location or something?
Warning CS0436
The type 'QrDataCommon' in '...DataAccess.cs' conflicts with the imported type 'QrDataCommon' in '...'. Using the type defined in '...DataAccess.cs'.
Most people likely run into this because of projects referencing themselves (as you pointed out); however, in your case it's because your VB project has a type with exactly the same namespace and name - a result of doing a direct 1:1 port from VB to C#.
Due to the name and namespace being identical C# has a choice to make: which one should it use? It is informing you that it is made the most logical choice and has chosen the one in your C# project - which is probably what you wanted it to do anyway.
These are your options:
Good option: Once you have completed porting a type to C# delete it from the VB project and recompile the VB project.
Good option: If you are not distributing your code as a standalone re-usable DLL (it looks like you are not), change the root namespace of your C# code.
Ignore the error until you have completed the port.
Worst option: Use a global namespace alias.
You have 2 DLLs using the same class name QrDataCommon. Rename one of them or don't reference QrCodes.dll.

C# using <myowndll>, doesn't work (VS10 Express)

Background: I am a novice in C#, and use Visual Studios 2010 Express.
I have a class (let's call it myclass) which I want to use in multiple projects. I used to add classes with Project->Add Existing Item... Which creates a copy of myclass.cs.
Now I just found out that when I build the original myclass.cs it creates a myclass.dll and places it in the release folder of my project.
But when I try to use this DLL, I get the following error:
The type or namespace name 'myclass' could not be found(are you
missing a using directive or an assembly refference?
Which is weird to me, because I already have referenced it (it is also in the Reference folder of my Solution Explorer). And I already have added this to my code:
using myclass;
So what am I doing wrong?
Update: When I tried my old method (add existing item -> myclass.cs) the error message goes away. So it's not a matter of spelling things correctly.
Add the dll first:
Click on references in your project-explorer in visual studio and add your dll then you can use it as you expected it.
Add the reference in your project and check that the target Framework version of that assembly fits the project.
Check the namespaces inside the assembly and then use them like:
using YourAssemblyNamespace.class
Okay so I found the answer myself. It turns out that when you use the using function, it automatically searches for all public classes in the namespace you want to use.
If it can't find a public class, it refuses to recognize the DLL.
Furthermore, not specifying a class makes it internal.
So:
class myclass // internal!
private class myclass // private!
public class myclass // only this makes it visible for others!
Everything was okay after changing class myclass into public class myclass.

why we give reference to the other projects?

When looking at a solution with multiple projects:
1) Why do we add a reference to the other project? Can't we just use inheritance?
2) After we add the reference by using Visual Studio, why do we have to add the project to the namespace system? For example: using myReferenceProject; I thought that the IDE would do that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myReferenceProject;
using System.Data.SqlClient;
using System.Data;
1) why we give reference to the other project? cant we just use inheritance???
They're two completely different concepts.
Adding a reference to another assembly basically says, "I want to be able to use some of this code. Please make the compiler aware that this code exists, and optionally copy it into the output directory so that it's present at execution time too."
How would you expect to use inheritance to create a class derived from some type if the compiler has no knowledge of that type?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system???
Because assemblies and namespaces are different concepts too. A using directive simply says to the compiler "When I refer to type Foo within my code, look in these namespaces to try to resolve it." That type could come from any of the assemblies you've referenced.
It's really important that you understand the difference between assemblies and namespaces. Even though they're often named similarly (Foo.Bar.dll often provides types in the namespace Foo.Bar) they're conceptually different.
The project is a self sufficent compilable unit, that has to compile into the valid assembly file. That's why you need esplicitly specifiy which referencies every project needs in order to be able to compile separately from others.
There is no any inheritance concept on projects level.
1) why we give reference to the other project? cant we just use inheritance?
This question makes no sense. What does inheritance have to do with project references. Can you elaborate?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system?
Because there's an inherent difference between an assembly referencing another assembly (which is what happens when you add a reference to the project) and the language knowing where to find a class which is what happens when you use the using directive in a file.
For example, suppose you create a class in your project called TextBox. Then in another file you want to use that class. How would the language know whether you are referring to your custom TextBox class or another one in a referenced assembly? The answer to that question is namespaces. By fully-qualifying the class name with its namespaces, you tell the compiler which class you're using.
The using directive is a way to specifying the namespace once per file instead of every time you use the class (or other classes in that namespace). So if you need to reference your TextBox class multiple times within a single file, you wouldn't want to have to write this every time:
MyCodebase.MyAssembly.MyNamespace.MyOtherNamespace.SomethingElse.TextBox
Instead, you include a using directive of the entire namespace, so you only have to write this:
TextBox

Referenced class lib doesn't seem

I have a class lib and I referenced it to another windows project. I added its namespace to my Form.cs (using SaglikNetClassLib;). they are seem unknown when I want to access to classes. I can see their properties and methods. But the complier says "Error 7 The type or namespace name 'SaglikNetClassLib' could not be found (are you missing a using directive or an assembly reference?), Do you have any suggestion?
KR,
Çağın
Are you targeting the .net Client Framework? Visual Studio let's you add references to incompatible assemblies, but then gives exactly that error.
Check your project settings to make sure you're targeting the full .net framework.
Also, check that the Ilalcar class is public and not internal (which is the default if it's only declared as class without any modifier)
You probably need an using statement to put the class into scope.
using SaglikNetClassLib;
C# won't auto suggest it if the project has not been rebuild. Also make sure the class library project has been build before using it in code.
Intellisense seems to lag behind a bit at times. Simply pressing f5 (run) sometimes rebuilds the project completely and simply runs or gives a better error message.

Categories

Resources