.Classes next to the namespase in a class - c#

I get this strange thing when I create a new class, Next to the namespace name I get a .Classes which all I can tell prevents me from creating an instance of the class and hides it.
I would include the screen shot but im not sure how I do this. Please help so you can better understand the question.
Thanks

The namespace within Visual Studio will depend on two things:
The default namespace defined within the project properties.
The folder structure within your project.
I think that within your project you have a folder Classes where you put (well as the name guesses) your classes. Due to this fact Visual Studio will automatically append this name to the default namespace.
So either manually change the namespace manually right after you added a new class or move the file one level up within your folder structure of the project.

Related

C# Set namespaces easly in Visual Studio 2019

I have a C# project and I am mapping a lot of classes from Json models.
Some of the names are colliding so I have to setup namespaces every time. I generate the classes with an online tool to create C# classes from JSON, so by default the classes don't have a namespace.
Is there any way, with right click on the class or some tool on Visual Studio 2019, that allows me to automaticaly select a bunch of classes and set a namespace for them? Or right click a class and set it's namespace as it's path in the filesystem from the workspace folder?
The problem is that right now, my only chocie is copying the namespace, pasting it on every class and surrounding the code with brackets again and again. I just want to make this proces easier.
Also I know it's not needed to place every single class in a namespace, but to prevent execution time errors and keep the code sorted, I prefer to do so.
You can Paste JSON As Classes in Visual Studio.
It will create the classes with the current namespace. Simply create a new class, "Paste JSON As Classes" and it will create every model from the JSON on the clipboard.
You can user Resharper to adjust namespaces of existing classes.
https://www.jetbrains.com/help/resharper/Fix_inconsistent_namespace_naming.html
# Francesc Bosch.
For changing the namespace of an existing class, you can right click on your current namespace and select Rename -> change to your new namespace-> click Apply.
If you have multiple depths of the namespace, Visual Studio will not allow you to type dots. However, if you copy and paste a dot, it will succeed despite the warning.
You could also move the class to the changed target namespace.
1.Right-click the name of the class you want to move -> select Quick Actions and Refactorings... -> click Move to namespace...
2.In the dialog box that opens, select the target namespace you'd like to move the type to.

How to set a namespace for custom control

I have been implementing this solution explained here to make an outlinedTextbox.
I have created a test project and added directly in the main namespace and it works.
Now I want to add it to a library (HelperLib) and want to use it in whatever program of mine I want. For example here the program is called pcdLoggerS2.
but when I add it to my xaml it says that
so it's a matter of namespaces.
Therefore I have add this
xmlns:local="clr-HelperLib"
in my definition of my Base:WindowViewBase but nothing has changed but it's really in that namespace!
--ADD FOR DAVID---
You need to make sure that you've added the HelperLib project as a reference to your test project.
Open PcdLogger and right click on References, and add that project as a reference. Until you do this, the XAML in your test project will not be able to find the correct assembly.
Additionally, when you reference a namespace, from another assembly, you need to add that information to the namespace declaration in your XAML.
(See: https://msdn.microsoft.com/en-us/library/bb514546%28v=vs.90%29.aspx)
As an example:
(I would suggest leaving local for your local namespaces)
xmlns:helper="clr-namespace:HelperLib;assembly=HelperLib"
EDIT:
Additionally, Visual Studio's intellisense is fond of telling you that the namespace, or class, does not exist until you have built the project. You may have to rebuild and/or close/reopen the xaml file for intellisense to cooperate again.
check Namespace of you class.
HelperLib its name of a class, but not namespace.
Try to write this:
xmlns:local="clr-namespace:HelperLib"

The type or namespace name does not exist - folders in C#

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.

Visual Studio won't find class in separate .cs file

I have two classes in separate files in a project I'm working on. Windows forms classes specifically. When I try to use one class from the .cs file, Visual Studio cannot find it. It underlines the line of code and asks if I'm missing a directive or assembly reference. I have no idea why it isn't seeing the other class, as both .cs files are in the same directory. Does anyone have any ideas as to why this would be happening and how to fix it?
Also, one of the .cs files is copied from a separate project, so I don't know if that caused a problem somehow.
Also, one of the .cs files is copied from a separate project, so I don't know if that caused a problem somehow.
You should check the namespaces involved. My guess is that the one from the other project is in a different namespace. So, if you've got:
namespace FirstNamespace
{
class Foo
{
private Bar bar = null;
}
}
and
namespace SecondNamespace
{
class Bar
{
}
}
Then in the first class you need:
using SecondNamespace;
to allow you to use Bar without any qualification.
Or - possibly better - you could just change them so they're both in the same namespace.
Better yet, you could avoid copying anything from a different project, and instead use class libraries for code sharing.
In my case namespace was fixed but still visual studio did not recognized classes.
I solve it by right click on the class file and click 'Include In Project'.
If you copied the second file from another project, it could be that the class it contains is in a different namespace.
Add a using <namespace> at the beginning of the other file...

MVC web application not recognizing custom class

In my test MVC web application, I've created custom class, which retrieves data from database. I have created also another folder in MVC app called Data and i have placed that data class in this folder. When i want to use this class from Controller and when importing with:
using TestMvcApp.Data;
i get an error:
The type or namespace name 'Data' does not exist in the namespace 'TestMvcApp'
(are you missing an assembly reference?)
Where can be the problem? Why it cannot find my class? I have tried a lot of things like placing that class in controller folder, but nothing helped yet. I created my test MVC app according to this article Using MvcContrib Grid in ASP.NET MVC Project Thanks for tips and answers.
EDIT: Name of my class is "ProductDB" and i have specified the namespace in it. I imported that class into project, than i created DATA folder, then i placed that class in there and finally i have changed its namespace name to TestMvcApp.Data! That class is from my another project.
I don't know why, but in my case when I added the custom class, Visual Studio 2013 didn't mark it to compile. Instead, it was marked as "Content".
I got to know it because I edited the .csproj and my class was placed just as "Include". I changed it to "Compile" and voilá, it worked!
You may use the "Build Action" class's file property in the Properties Window.
Did you create the class before or after you put it in the "Data" folder? If it was before, does it still just belong to the TestMvcApp namespace, or did you change its namespace to be TestMvcApp.Data? If not, you need to either change the namespace the class is in or change the using statement.
If your class is called Data and your namespace is called TestMvcApp.Data I suspect that there is a naming collision.
You must also set the namespace of the class to TestMvcApp.Data

Categories