I am creating a dll project. In this project I have a main namespace, for example myClasses. In this project, I create a new folder, myClasses2, so I have a second namespace, myClasses.myClasses2. When I create the dll, I get the myClasses.dll.
In other project, for example in a WPF project, I add a reference to this dll, and I can do "using myClasses" but I can't do "using myClasses.myClasses2" so I can't use this second group of classes.
There is any way to have, in one dll, the two namespaces? I would like to avoid to have two separate dlls, one for myClasses and other for myClasses2.
EDIT1: Thanks to all. The problem is that the classes are not public. I use the default when add a new class. When I set the class as public and generate again, I can using the second namespace.
Thanks.
Related
I'm learning C# and I don't know exactly how to make a class visible to all projects inside the Solution. Basically I have many projects, all Windows Forms, all of them have some similarity, so they can share a lot of classes. I think a good approach is put all the shared classes in the Solution folder and make all the apps get the classes from there. Is this a good approach? How to do it? Another way to do it, I think, is to put all the shared classes in one app and the others get that references.
If that is not a good approach, which one is? How to do it?
I'm using Visual Studio 2019. This is how my Solution looks like, but I can't share the classes between the projects:
Create a Class Library project within your solution and put your shared classes in there (right click on your solution in the Solution Explorer, Add, New Project). Then for each of the projects that you want to access the classes from, right click on Dependencies, Add Project Reference and tick the Class Library project you created.
You create a class library project and in there you would want to add folders where you can name them according to what the classes would do so it is easier to maintain. Then you would add your classes in the corresponding folder and in the project that you want to use the class you can add a reference to the project and the class would be accessible as long as it was a public class.
I am new to C# and want some help regarding some small task.
I have two projects (Project1 and Project2) which are dependent on each other. I would like to call the methods from Project1 which is used as reference in Project2 without creating object of Project1. This means that I don't want to use Project1 directly because if I do so and some changes are made in Project1 I will have to recompile Project2 as well.
Can we create any intermediate project (say Project3) which will work as bridge between this two projects? So that Project3 can be used in some other task if needed. If this is possible then please let me know how?
I have stuck due to this.
I'm trying to do this so that I can access a certain part of the other linked project in all other projects. right now when I switch between projects I can only land on their entry points.
I have added a reference from my main project to two other dependent ones
Put the common parts in a separate DLL (aka "Class Library" project in Visual Studio). Then add references to that project to the other projects where you need to use them. Don't forget to make the common classes public.
In such case mostly people add one common project in there application and do stuff there which can be used in any other project to avoid the circular reference.
and of your public class will be available to any other project.
From a solution, which I made in Visual Studio 2013, I want to transfer a method to a DLL (class library project).
When I paste the method in the DLL project, it doesn't recognize parts of the code and it's showing this error`:
"are you missing a using directive or an assembly reference?"
Because of that, the DLL can't be built. I am assuming, that a certain reference to the solution is required, but I am not sure how to proceed.
I know how to add a reference from a solution to a DLL, but I'm not sure how it's done the other way around or even if it's possible.
You can't add reference to EXE from class library (assuming EXE uses that class library) as it will introduce circular reference.
Usually you need to refactor all dependencies so class library either have them all or allow application to inject dependencies from EXE/other clients. In later case class library needs to define base classes/interfaces to allow such injection.
Yes, you need to restore the same references that the original project uses, if they are used in the code you want to move.
If you need to do this by hand (i.e. without tools like ReSharper):
Move the code to the new assembly.
For each namespace or type giving the error, find it in the Object Browser.
Locate the assembly containing that namespace and type, and add a reference to that assembly in your new project.
You may also have to add a Project Reference to the original project.
A beginner's question. It is c#.
Let's say I have three classes in one project named Employee, Department, Address. For some reason, I would like to have a .dll file (let's name it test.dll) to have all three classes included that I can call it from some other project using syntax like "test.Employee emp1 = new test.Employee();"
That is my idea. Is this possible? If yes, how should I do that? Do I have to create a class library project to do so? I know nothing about a class library project. So I may need further help with that.
If the answer is no, how do I add references to those classes from other solutions?
Thanks.
Create a class library project.
Give it a proper namespace
Write your library classes, then compile to a dll
Then add a reference to that dll in the other project you want to use it in
add a using statement to include the reference in your code files.
Pretty straightforward