How namespace works in ASP.NET

9

I do not know ASP.NET, but I had to support a very disorganized third party code by the way.

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

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

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

Namespace Sitefeito.Mkrbpg

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

Namespace Sitefeito.Mkrbpg

So, which of the files are he calling?

Since I've already deleted all three, the code still works. However, if I change the Namespace it gives it error.

    
asked by anonymous 13.05.2014 / 16:09

2 answers

12

It seems like 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 namespace X and class Y . It's the same as saying that you declared class X.Y .

Import

When you put 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 import directive). So the compiler understands that you have used the Y class in your code and have import X . Everything will work because it finds the class, it has the full class name.

When you are going to use a member of a class you can refer to a method Z as X.Y.Z() and do not use import . This way it becomes clear that namespace is just a last name of the class.

Namespace is not encapsulation

So you can have% s of% s repeated everywhere in your software. He does not compartmentalize anything. In .Net this compartmentalization occurs through the assembly . That is, roughly speaking, the equivalent of namespace of Java. In fact, package also organizes the surname of the type at the same time. The surname of the type is the same as the package where it is.

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

The package transcends namespace and is orthogonal concepts. Effectively when the code is compiled, assembly disappears and only one type with a surname is in the code.

Disambiguation

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

But if you have the same class in the same namespace , some error should occur. So much so that the only way to have twice the same name for a class is to use namespace which indicates that in different sources you have the same complementing class. But it does not seem to be your case.

Assembly X namespace

The fact that there is an assembly and a partial with the same name, is just a coincidence. These are very different things. The assembly is referenced in the project (it may be that the problem is there).

Your case

If you took the% s of% s and no problem happened, you were not using any type contained in the% s 'imported'% s. If you change the name of namespace that is really needed, it will give error.

It is possible that some of these files that you say have duplicate classes are not being referenced in the project. It could be that it was rubbish, test, I do not know, that the previous programmer left there without necessity.

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

For more information you can read this and this response.

    
13.05.2014 / 18:08
4

According to documentation :

  

Namespaces organize the objects defined in an assembly. Assemblies can 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.

So, several files can contain the same namespace, and the file that "is called" would be one containing a class, contained in the namespace, that is used on the page in question.

    
13.05.2014 / 16:41