If you use a you assembly reference (myExample.dll), you add like this to the top
using myExample;
Now if you create a class file, how do you reference it?
Well, in your class file you have the following:
namespace myNamespace
{
public class MyClass
{
public void MyMethod() { }
}
}
Let's assume that you have this in an assembly named MyDll.dll. You'd use it as follows:
You add a reference to MyDll.dll within the solution explorer
You include the namespace with using myNamespace;
Then you can use your class doing MyClass test = new MyClass();
If you don't add the namespace like I said in 2., you'd use your class like:
myNamespace.MyClass test = new myNamespace.MyClass();
You want to add a using statement for whatever namespace you want to import. Go to the file and see what namespace it wraps the class you're interested in.
You just include the file when compiling the assembly.
You may have to add a using statement too.
Your question is somehow unclear.
When you define a new class, in another dll, it is enough to reference that dll.
Please note that you might be unable to access that class because of its accessors. Define your class with a public keyword.
Related
I'm pretty new to the whole C# thing and I'm a bit used to C++ with the header files. So I hope this isn't the dumbest question.
How can I import the MainWindow.cs into the MainWindow.xaml.cs?
I thought I could just use using MainWindow.cs
In .Net you can use any class in the same project, or in any referenced project or dll. You never need to worry about header files. The .Net assemblies themselves contain all the type information needed to use them.
Type disambiguation is done with namespaces, you should typically place types in a namespace like ProjectName.Folder I.e.
namespace MyProject.Quelldateien.Inc{
public class MainWindow{
...
}
}
In your code file you need to use the full namespace when refering to a type, i.e. MyProject.Quelldateien.Inc.MainWindow, or add a using statement at the top of your file:
using MyProject.Quelldateien.Inc
namespace MyProject{
public class MyClass{
...
}
}
Note that you should usually try to avoid using the same name for classes, since this would require using the full namespace to refer to your type. A common convention is for your name your xaml-classes MyMainView and the view model class (containing properties you bind to) as MyMainViewModel. And place this in separate folders.
Also note that some refactoring tools can add namespaces automatically, so I almost never write out using MyProject.Quelldateien.Inc, I just write my type name, and accept the namespace suggestion by the tool.
in the MainWindow.cs see the namespace. like:
namespace Quelldateien.inc
{
internal class MainWindow
{
}
}
then use:
using Quelldateien.inc;
If I am making a .Net dll, is it possible to break encapsulation in any program that uses it, by creating a class with the same namespace?
For example, consider a DLL with the following code in it:
using System;
namespace MyDLL
{
internal class MyClass
{
internal void Stuff()
{
...
}
}
}
If I was to then create a project, and reference the DLL, would I be able to do something like the following?
using System;
using MyDLL;
namespace MyDLL
{
public class TheirClass
{
MyClass exposedClass = new MyClass();
exposedClass.Stuff();
}
}
I'm currently working on a project that will have a few abstract classes I want a user to be able to inherit, but I only want to certain features to be exposed.
Thanks in advance.
No, using the same namespace have no impact on data encapsulation nor any impact on visibility (private/protected/internal). Namespaces are syntactic sugar in C#, actual class names consist of namespace, name and assembly identity.
So in your particular case adding class with full name MyDLL.TheirClass{TheirAssembly} will not make MyDLL.MyClass{MyDLL} visible from TheirClass (since your class is internal and new class is from other assembly not marked as InternalsVisibleTo in your assembly).
If we create private class under any namespace then we got compilation error but if we create private class as a nested class in another class then it compile fine. Another guy explained why we got an error for declaring private under any namespace ? He said:
Allowing classes to be private to a namespace would achieve no
meaningful level of protection.Any assembly in the world could simply
reference your dll, and start writing code in your namespace which
accesses your supposedly private classes.I think that's possibly the
answer you'd get from Microsoft.
I just really do not understand this sentence "start writing code in your namespace which accesses your supposedly private classes"
How can any one access my private class? Any can tell me this "start writing code in your namespace which accesses your supposedly private classes"
Suppose you have one DLL where you wrote:
// hypotetical code !
namespace My.Namespace
{
private class MyClass
{
}
}
By this you want to declare that this class can only be used withing the namespace My.Namespace. You want to make this class internal to that namespace and protect if form usage outside.
Now anyone could create a different DLL reference the above DLL and declare the same namespace My.Namespace and actually be able to use MyClass anyway. Because its in the same namespace:
// hypotetical code !
namespace My.Namespace
{
public class AnotherClass
{
private hisClass = new MyClass();
}
}
What use is the private modifier in this case?
Imagine the following scenario:
You write an assembly A.dll with a namespace Mou.MyStuff. In that namespace, there is a private class SomeClass. To whom should that class be visible, based on its private visibility? Only to other types in the same namespace?
Then, someone else can write his or her own assembly B.dll. In that assembly, they can define a class SomeOtherClass in namespace Mou.MyStuff. Nothing prevents them from using the very same namespace as you did, hence there would be absolutely no obstacle to accessing your "private" type.
If what you are looking for is visibility only within your assembly, use the internal visibility.
I'm running into an issue where I can't make a reference to a class in a different namespace. I have 2 classes:
namespace Foo
{
public class Class1 { ... }
}
namespace My.App.Foo
{
public class Class2
{
public void SomeMethod()
{
var x = new Foo.Class1; // compile error!
}
}
}
The compile error is:
The type or namespace name 'Class1'
does not exist in the namespace
'My.App.Foo'
In this situation, I can't seem to get Visual Studio to recognize that "Foo.Class1" refers to the first class. If I mouse-over "Foo", it shows that its trying to resolve that to "My.App.Foo.Class1"
If I put the line:
using Foo;
at the top of the .cs file that contains Class2, then it also resolves that to "My.App.Foo".
Is there some trick to referencing the right "Foo" namespace without just renaming the namespaces so they don't conflict? Both of these namespaces are in the same assembly.
You can use global:: to globally qualify a namespace: global::Foo.Class1 should work,.
You could also alias global::Foo to make things easier. At the top of your source file, below your using statements, add:
using AliasClass1=global::Foo.Class1;
Now you can use:
AliasClass1 c = new AliasClass1();
// and so on.
Of course, you can use a better name than AliasClass :-)
var x = new global::Foo.Class1();
In addition to LBushkin's answer, you might be interested in these articles by Eric Lippert :
Do not name a class the same as its namespace, Part One
Do not name a class the same as its namespace, Part Two
Do not name a class the same as its namespace, Part Three
Do not name a class the same as its namespace, Part Four
They are not directly related to your problem, but they give an interesting insight on naming strategies
I have a problem here. I have a class library namely ClLib, and has the following 8 cs files.
Express (parent class)
- 1E1
- 1E2
- 1E3
Normal (parent class)
- 1N1
- 1N2
- 1N3
Also, a method is also included in each 8 class file. For example,
public class 1E1:Express
{
public int subtractNumExp1E1(int firstNum, int secNum)
{
return firstNum - secNum;
}
}
The 1E1:Express is used to display the relationship between the parent class(Express) and subclass (1E1).
I have also created a Windows App in Visual Studio, and I need to create a directive like "using ClLib.Express.1E1". How do I go about it? I am stuck in this situation whereby when I want to change the namespace of 1E1 from "namespace ClLib" to "namespace ClLib.Express.1E1". An error occurs, as the namespace ClLib has already contain one definition for Express.
A great thanks and appreciation in advance for all kind souls who are willing to help me:)
You can't have a type and a namespace with the same name ("CILib.Express").
If you want a CILib.Express namespace, the simplest option is to move/rename the CILib.Express type.
You can, however, also have nested types. You could, for example, have:
namespace CILib {
public class Express {
public class SomeType {}
}
}
If separate files are a concern:
file 1:
namespace CILib {
public partial class Express {
// "Express" code
}
}
file 2:
namespace CILib {
public partial class Express {
public class SomeType {
// "SomeType" code
}
}
}
However, using directives only relate to namespaces; I don't think you could have using CILib.Express, as that is a type not a namespace.
In that case you just have to delete the namespace ClLib and make sure that there is exactly one copy of ClLib.Express.1E1
P/S: Are you sure that the proper namespace is ClLib.Express.1E1? 1E1 is a cs file, and it's usually a class. So you don't have to write using ClLib.Express, that would be sufficient.
I think there is confusion around the namespace and base class.
Namespace: ClLib.Express
Class: public class 1E1:Express
Rename your base class to ExpressBase to differentiate it.
If you need to reference the namespace:
using ClLib.Express
Hope I have understood your project structure!