How the "namespace" works in ASP.NET

Asked

Viewed 949 times

12

I don’t know ASP.NET, but I had to support a very disorganized third party code by the way.

I have a file xxxx.aspx that is in the folder /cp and have those calls:

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Threading" %>
**<%@ Import Namespace="Sitefeito.Mkrbpg" %>**
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="Microsoft.VisualBasic" %>

Also in the folder /cp have file yyy.asp that has

Namespace Sitefeito.Mkrbpg

Inside of another folder /cp/vb I have the files zzz.aspx and another www.vb both also with:

Namespace Sitefeito.Mkrbpg

So, which one of the files is he calling?

Since I’ve already deleted all three and still the code keeps working. However, if I change the Namespace He’ll make a mistake.

  • Do you know Java? as namespace are for C# the same thing as the packages are for Java as far as I remember.

  • I know a little bit about java yes. And as far as I understand the namespace was to call yyy.Asp because it’s the path equivalent. Since the namespace does not have a full path.

  • @Andrade They are not, there is a huge conceptual difference between namespaces and packages. In C# the equivalent of packagewould be the assembly.

  • @bigown I heard this once in a discussion about the differences and equivalences between C# and Java, but could not say, so I commented that as far as I remember was this. I’ll take a look at the concepts

  • In fact, I was wrong too, because the package is not the same as the assembly, after all it carries a concept of namespace together. Package and type naming get mixed up in Java. In C# or VB.Net it is that they are totally separated.

  • You can accept one of the answers if it helped you. None helped? There is something in my answer that I would need to improve to meet 100% of what you need?

Show 1 more comment

2 answers

12

It seems that the confusion here is about the conceptualization of what is namespace.

A namesapace is a surname for a type (a class for example). If you have a namespace X and a class Y. Is the same as saying you stated class X.Y.

Import

When you put the import X, you are just telling the compiler that it should search for types (classes for example) used in your code in that file (technically in any code that is below the directive import). Then the compiler understands that you used the class Y in its code and has a import X. Everything will work because he finds the class, he has the full name of the class.

When using a class member you can refer to a Z method such as X.Y.Z() and not use the import. This way it becomes clearer than the namespace is just a class surname.

Namespace is not encapsulation

So you can have repetition of namespaces everywhere in your software. It does not compartmentalize anything. In . NET this compartmentalization occurs through the Assembly. Which is roughly the equivalent of package of Java. Actually the package also organizes such last name of the type at the same time. The last name of the type is the same of the package where it is.

A package has path, as well as the Assembly. But in . NET this does not matter in the code. The Assembly is inserted in the project externally to the code. C# and VB.NET separated the concept of code package and type grouping (classes, enums, etc.).

The namespace transcends the assembly and are orthogonal concepts. Effectively when the code is compiled, the namespace disappears and only one guy with a last name is in the code.

Disambiguation

If your code has the same class in namespacedifferent s, you need to use the full name (you can even use the import to create an alias for a namespace to facilitate).

But if you have the same class in it namespace, some error must occur. So much so that the only way to have twice the same name for a class is by using partial which indicates that in different sources you have the same complement each other. But it doesn’t seem to be your case.

Assembly X namespace

The fact that there is a Assembly and a namespace with the same name, it’s just coincidence. They’re very different things. The Assembly is referenced in the project (it may be that the problem is there).

Your case

If you took the imports and no problem happened, you were not using any kind contained in namespaces "imported". If you change the name of namespace that really is necessary, will give error.

It is possible that some of these files that you say have duplicate classes are not even being referenced in the project. Could be it was garbage, test, I don’t know, that the previous programmer left there unnecessarily.

If you need more specific information, we need to better understand how your project and code are structured.

For more information you can read that one and that one reply.

5

According to the documentation:

Namespaces organize objects defined in an Assembly. Modules (assemblies) may contain multiple namespaces, which in turn may contain other namespaces. Namespaces avoid ambiguity and simplify references when using large groups of objects such as class libraries.

Thus, several files may contain the same namespace, and the file that is "called" would be one that contains a class, contained in the namespace, which is used on the page in question.

  • That’s right, my friend, but the zzz.aspx files, zzz.aspx and www.Vb have the same class and the same namespace so I don’t know what the priority he gives and the weirdest thing is that if I delete the three files it continues to work and I’ve searched throughout my project and only find these three classes in the files I mentioned

  • 1

    There is something very wrong there, because it is not possible to have more than one class with the same name in the same namespace. Does what is working is not referencing to a dll, which would be the entire website compiled? Have you tried to open the project itself and do these tests, instead of just manipulating the files . aspx and . Vb ?

  • My dear it seems is this yes, I found here a . dll and . PDB with the same namespace name. How do I manipulate the same???

  • You don’t have access to this dll Project? With this project you could manipulate it, otherwise it becomes more complicated, but it is possible that programs like Reflector (http://www.red-gate.com/products/dotnet-development/reflector/) help you in this matter.

  • 2

    There is also http://www.jetbrains.com/decompiler/

  • @bigown can uncomplicate and extract the code I made the change, there is some option to recompile lighter than the visual studio?

  • @Not much of a catch. You can compile from the command line using, I know that in C# is csc.exe (I don’t know about Vb.net, I’ve never used, but it’s inside the .Net folder inside Windows), but the difference will be very small.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.