03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

184 x CHAPTER 5 PROGRAMMING YOUR <strong>ASP</strong>.<strong>NET</strong> WEB PAGES<br />

Organiz<strong>in</strong>g Code with Namespaces<br />

Namespaces seem to cause a lot of confusion with new developers. They th<strong>in</strong>k they’re scary, they<br />

th<strong>in</strong>k way too many of them exist, or they don’t see the need to use them. None of this is true, <strong>and</strong><br />

with a short explanation of them, you’ll underst<strong>and</strong> <strong>and</strong> maybe even like namespaces.<br />

Namespaces are <strong>in</strong>tended to solve two major problems: to organize the enormous amount of functionality<br />

<strong>in</strong> the .<strong>NET</strong> Framework <strong>and</strong> <strong>in</strong> your own code, <strong>and</strong> to avoid name collisions, where two<br />

different types share the same name. One common misconception about namespaces is that there is<br />

a direct relation with .<strong>NET</strong> assemblies (files with a .dll extension that are loaded <strong>and</strong> used by the<br />

.<strong>NET</strong> Framework), but that’s not the case. Although you typically f<strong>in</strong>d namespaces like System<br />

.Web.UI <strong>in</strong> a DLL called System.Web.dll, it’s possible (<strong>and</strong> common) to have multiple namespaces<br />

def<strong>in</strong>ed <strong>in</strong> a s<strong>in</strong>gle DLL or to have a namespace be spread out over multiple assemblies. Keep that <strong>in</strong><br />

m<strong>in</strong>d when add<strong>in</strong>g references to assemblies, as expla<strong>in</strong>ed later.<br />

To see what a namespace looks like, open one of the Code Beh<strong>in</strong>d files of the <strong>ASP</strong>X pages you’ve<br />

created so far. You’ll see someth<strong>in</strong>g similar to this:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Partial Class Demos_CalculatorDemo<br />

Inherits System.Web.UI.Page<br />

C#<br />

public partial class Demos_CalculatorDemo : System.Web.UI.Page<br />

{<br />

Note that the def<strong>in</strong>ition of the class name is followed by the Inherits keyword <strong>in</strong> <strong>VB</strong> <strong>and</strong> a colon <strong>in</strong><br />

C#, which <strong>in</strong> turn are followed by System.Web.UI.Page. You see later what this Inherits keyword<br />

is used for. In this code, Page is the name of a class (a data type), which is def<strong>in</strong>ed <strong>in</strong> the System<br />

.Web.UI namespace. By plac<strong>in</strong>g the Page class <strong>in</strong> the System.Web.UI namespace, developers (<strong>and</strong><br />

compilers) can see this class is about a web page. By contrast, imag<strong>in</strong>e the follow<strong>in</strong>g (fictitious) class<br />

name:<br />

Microsoft.Word.Document.Page<br />

This code also refers to a Page class. However, because it’s placed <strong>in</strong> the Microsoft.Word<br />

.Document namespace, it’s easy to see that it’s referr<strong>in</strong>g to a page of a Word document, not a web<br />

page. This way there is no ambiguity between a web page <strong>and</strong> a Word document page. This <strong>in</strong> turn<br />

helps the compiler underst<strong>and</strong> which class you are referr<strong>in</strong>g to.<br />

Another benefit of namespaces is that they help you f<strong>in</strong>d the right data type. Instead of display<strong>in</strong>g<br />

thous<strong>and</strong>s <strong>and</strong> thous<strong>and</strong>s of items <strong>in</strong> the IntelliSense list, you get a few top-level namespaces. When<br />

you choose an item from that list <strong>and</strong> press the dot key (.), you get another relatively short list with<br />

types <strong>and</strong> other namespaces that live <strong>in</strong>side the chosen namespace.<br />

Namespaces are noth<strong>in</strong>g more than simple conta<strong>in</strong>ers that you can refer to by name us<strong>in</strong>g the dot<br />

notation. They are used to prefix each data type that is available <strong>in</strong> your application. For example,<br />

the Double data type lives <strong>in</strong> the System namespace, thus its fully qualified name is System<br />

.Double. Likewise, the Button control you’ve added to your web pages lives <strong>in</strong> the System.Web<br />

.UI.WebControls namespace, thus its full name is System.Web.UI.WebControls.Button.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!