I have two projects called Hbt.DMS.BusinessLogic and Hbt.DMS.Web . In Hbt.DMS.BusinessLogic I have a folder called Operations. When I try to create a new file inside Operations folder, it generates a wrong namespace inside the file.
ex: the generated namespace: Greenwich.DMS.Web.Operations
expected namespace: Greenwich.DMS.BusinessLogic.Operations
I noticed that this happens when I try to create new files in any folder in Hbt.DMS.BusinessLogic project.
What am I missing?
Make sure you have the correct Default namespace in the Project Properties (e.g. Greenwich.DMS.BusinessLogic). The visible name of the project does not necessarily have to be the same as the default namespace.
Related
I was following a tutorial from Microsoft found at https://learn.microsoft.com/en-us/training/modules/create-razor-pages-aspnet-core/
At one point it instructs to add a folder under the root project called "Services". Within that folder is a file which needs to be accessed. Upon creating the project, another folder called "Models" was automatically created under the root folder.
I include the two lines at the top of another file:
using RazorPagesDoughnuts.Services; using RazorPagesDoughnuts.Models;
The Models statement works no problem. The Services statement generates the error. I have searched many resources and cannot find a solution.
I am using vscode 1.71.2 and .net 6.0
Screenshot of file structure and statements
The namespace provided in the file DoughnutService.cs is probably not correct. Change that to RazorPagesDoughnuts.Services and check.
In C# using statements are not based on file structure, but rather on namespaces. If there aren't any files with the statement namespace RazorPagesDoughnuts.Services in the project, then you will not be able to reference it in other files.
Make sure that DoughnutService.cs contains this namespace statement.
I have a problem understanding the difference between namespaces and assemblies.
So let's say that I make open Visual Studio, and I create a new project. I will name the project "Project A". The Solution Explorer will look like this:
Now, as far as I understood, the "Solution 'Project_A'(1 project)" is the assembly & "Project_A" that is right under it is the first namespace. Now, I know that I can add multiple "nested" namespaces with different classes. So I can make another class called X and then make a new folder in "Project_A", so a new namespace that will be called "MainClasses" and add the classes A & B there so that it would look like this:
So now, if I'm not wrong: I have the assembly "Project_A" that has the namespace "Project_A". The namespace "Project_A" includes a class called X & another namespace with classes A & B.
Now, if I go to "Solution 'Project_A'(1 project)" and I click on Add->New Project, I will make a new namespace with the name "Project_B", and add another class to the new namespace called Y, I will now have:
The assembly "Project_A" that will contain the namespace "Project_A" & "Project_B", and it will look like this:
Can somebody please correct me if I am wrong and tell me the right way. So what is the exact difference between namespaces & assemblies when working with c# in visual studio. Showing some screenshots would be the best, if you can do it, of course. Thank you
An assembly is an exe (executable) or a dll (dynamic link library) and it is a software primary "component" (not in the sense of OOP component or control). Sometimes named package.
What exactly is an Assembly in C# or .NET?
https://learn.microsoft.com/dotnet/standard/assembly/
A namespace is a code organization feature.
https://www.tutorialspoint.com/csharp/csharp_namespaces.htm
https://learn.microsoft.com/dotnet/csharp/programming-guide/namespaces/
An assembly that is like a partition can contains one or more namespaces that are like folders.
When an assembly is added to the references of the project, you can access to all its namespaces with the using directive at the beginning of the file or by specifying full access directly in the code.
Example
The assembly System.dll contains several namespaces like System and System.IO as well as System.Threading and so on.
using System.IO;
var lines = File.ReadAllLines(...);
Or:
var lines = System.IO.File.ReadAllLines(...);
...
These two concepts are not related. It's only by default that your initial namespace takes the name of your project.
By default each of your projects, contain the global namespace and your own. You can rename the default name of your namespace to anything you want without an issue.
The assembly:
Assemblies form the fundamental units of deployment, version control, reuse, activation scoping, and security permissions for .NET-based applications. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (.dll) files, and are the building blocks of .NET applications. They provide the common language runtime with the information it needs to be aware of type implementations.
The namespace:
Namespaces have the following properties:
They organize large code projects.
They are delimited by using the . operator.
The using directive obviates the requirement to specify the name of the namespace for every class.
The global namespace is the "root" namespace: global::System will always refer to the .NET System namespace.
The namespace for the project is set in the project properties. It is by default set to the assembly name and class files inherit this name when created, but you can change to any name you like. If you add a folder and put a file in it, the folder name gets appended to the parent (for the first folder this is the assembly) namespace. Again you can change this to any arbitrary name.
At this moment the default namespace for resources (resx files) is ProjectName.Resources.
I would like to change this default to being just Resources.
What I have tried to do here is manually change the namespace in Address.Designer.cs. While that seems to work at first, the default is reset after changing one of the values in the resx file.
How can this be done?
It seems you cannot change the namespace as easily as it was in old ASP.NET projects just by changing the Custom Tool Namespace in the properties section of a resx file. Simplest thing you can do is create a Class Library project named Resources in your solution and add Address.resx to it. Then change its Access Modifier to Public.
I'm sure there is a simple solution to this problem, but it has defeated me so far. Basically all I'm trying to do is include some classes in a separate folder in my c# project.
Strangely this has been working just fine until today.
In the solution explorer I created a new folder called animations.
I added the line to the main class:
using AnimationEditor.animations; (AnimationEditor is the solution name/namespace)
which throws the error:
Error 1 The type or namespace name 'animations' does not exist in the namespace 'AnimationEditor' (are you missing an assembly reference?)
As I said, I didn't have this error before today so I am a little confused.
If you don't have the the line namespace AnimationEditor.animations in the the class you are trying to reference you need to manually add it.
Those namespace statements do not get automatically added when you move a file, they only get automatically put in when you create a new file under the folder.
So your class should look like
namespace AnimationEditor.animations
{
class MyClass
{
//snip
}
}
As a side note, the .NET naming conventions state you should use a capital letter for those sub namespaces, capitalize the folder name and it will automatically capitalize the namespace for new files (you will need to manually change existing ones, just like moving)
If you drag files to another folder (or add them) in visual studio, namespace does not change automatically (atleast in 2010). Check namespace of AnimationEditor class.
I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.