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
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'm new in C# and I'm now coding with Visual Studio 2013.
I'm developing a project and it can be divided into several functions.
When I want to separate the code into different packages like what I did with Java, I find that Visual Studio only offer me to create a new project in the same solution.
So does C# have anything like package in java? So that I can place my source properly. Or the project itself is just like packages in java?
Thank you very much!
No,
C# has namespaces. You can normally wrap objects (classes, enums, structs, etc) in namespaces
namespace Whatever
{
public class ClassA{}
public struct StructA{}
}
Then to instantiate a ClassA do the following...
Whatever.ClassA _classA = new Whatever.ClassA();
You can also at a "using" directive so you don't have use the namespace all the time in your code file....
using Whatever;
ClassA _classA = new ClassA();
Hope it helps,
Leo
As MSDN state:
The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.
Instead of package c# using namespace.
for java you use the jar file here dll.using the namespace you can access the dll's.
you can separate the class file using the folder also.you need to create the folder under the project.
I think you are trying to separate some sort of code from other.
you can use namespaces for doing that.
please find below link.
http://msdn.microsoft.com/en-us/library/z2kcy19k.aspx
There are a lot of variables and methods in my program and I want to seperate some of them in other class files. But as the program grows the methods and functions can change.
I searched on the net but many people generally speaking for dll files. Without making a dll file, how can I arrange my code and split into small class files?
Yes, just split it out in to a separate file in a new class but still inside the same project. The term for what you are doing is called Code Refactoring. There are some tools built in to Visual Studio to make it easier to do, and there are some 3rd party tools that add even more features to make it easier to do.
But all it boils down to is just making new classes in the same project and referencing those new classes from where you took the code out from.
You can add folders to your solution. Classes are by default a namespaceprovider, so that classes in this folder have a different namespace.
For example if your default-namespace is MyNameSpace and you create a folder called Entity then all classes in this folder have the namespace MyNameSpace.Entity
And all Items in a project are compiled to one single dll or exe
Just add more classes to the project and put the data and behavior (methods) into the appropriate classes. The project will still build into a single exe or dll.
Generally, it's better to add a second project under the same solution call it "CommonLib" or something like that. Then you add it as a reference to the main application and set up the project so that the applications build depends on the libraries build. Add a using statement for the common lib where ever you want to use those objects. This is definitely better for large scale or enterprise applications. There's a pretty decent chance that somewhere down the line you'll want to reuse some of this code, if everything builds into a single exe that won't be an option.
How can I create a type in vb.net and use the type in c#?
I guess I need to compile the vb project and add reference to the dll but have no idea if this is the right way to do it, or the reference will be valid.
Doing this is not for fun, we have some vb code and we're considering this option.
Just create a Class Library project in VB, and then add a reference to that project from your C# project. (Or if it's in a different solution, add a reference to the DLL created by the VB project.) It should be absolutely fine - you'll have access to all the Public types from your class library.
To address the comment in your question, the projects can both default to the same namespace, but it's generally a better idea to make each project have its own namespace, so it's clear where any particular type comes from.
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.