03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Data Types <strong>and</strong> Variables x 155<br />

Clear methods to remove one or all items from the collection. Just like arrays, they enable you to<br />

iterate, or loop, over them to access the items <strong>in</strong> the collection.<br />

When collections were first <strong>in</strong>troduced <strong>in</strong> the .<strong>NET</strong> Framework 1.0, the ArrayList <strong>and</strong> Hashtable<br />

became popular very quickly because they were so easy to use. The ArrayList enables you to add<br />

arbitrary objects that are then stored <strong>in</strong> the order <strong>in</strong> which you add them, whereas the Hashtable<br />

enables you to store objects referenced by a custom key. The ma<strong>in</strong> benefit of these collections over<br />

their array cous<strong>in</strong>s is that they can grow on dem<strong>and</strong>. Unlike the previous example, where you<br />

needed to resize the array to create room for the third role, the ArrayList grows dynamically when<br />

required. The follow<strong>in</strong>g example shows you how this works:<br />

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

Dim roles As New ArrayList()<br />

' Create a new ArrayList. You don't need<br />

' to set its size explicitly<br />

roles.Add("Adm<strong>in</strong>istrators")<br />

roles.Add("ContentManagers")<br />

roles.Add("Members")<br />

' Add the first role<br />

' Add the second role<br />

' Keep add<strong>in</strong>g roles <strong>and</strong> the ArrayList<br />

' grows as necessary<br />

C#<br />

ArrayList roles = new ArrayList(); // Create a new ArrayList. You don't need<br />

// to set its size explicitly<br />

roles.Add("Adm<strong>in</strong>istrators"); // Add the first role<br />

roles.Add("ContentManagers"); // Add the second role<br />

roles.Add("Members");<br />

// Keep add<strong>in</strong>g roles <strong>and</strong> the ArrayList<br />

// grows as necessary<br />

Because this code now calls a method (Add) rather than assign<strong>in</strong>g an item to a predef<strong>in</strong>ed <strong>in</strong>dex <strong>in</strong><br />

an array, you need parentheses (()) <strong>in</strong> both <strong>VB</strong>.<strong>NET</strong> <strong>and</strong> C#. The usage of methods is discussed<br />

later <strong>in</strong> this chapter.<br />

Although collections solve some of the problems that arrays have, they <strong>in</strong>troduce a few problems of<br />

their own. The biggest drawback of the ArrayList is that it isn’t strongly typed. What this means<br />

is that you can add any object to the list us<strong>in</strong>g the Add method. This means that the ArrayList<br />

could hold objects that are of different types at the same time. This may not seem to be a big deal<br />

at first, but as soon as you start work<strong>in</strong>g with an ArrayList that conta<strong>in</strong>s multiple types of objects,<br />

you’ll quickly see why this is problematic. Take the roles example aga<strong>in</strong>. With the array <strong>and</strong> the<br />

ArrayList versions, the code simply added a few str<strong>in</strong>gs conta<strong>in</strong><strong>in</strong>g role names. You can then use<br />

these three str<strong>in</strong>gs to, say, build up a drop-down list <strong>in</strong> a Web Form to enable a user to pick a role.<br />

So far, so good. But what if one of the items <strong>in</strong> the list is not a str<strong>in</strong>g? What if another developer<br />

accidentally wrote some code that adds a DropDownList control to the ArrayList? Because the<br />

ArrayList accepts all objects, it won’t compla<strong>in</strong>. However, your code will crash if it expects a<br />

Str<strong>in</strong>g, but gets a DropDownList control <strong>in</strong>stead.<br />

With .<strong>NET</strong> 2.0, Microsoft <strong>in</strong>troduced a concept called generics. Generics are still strongly present<br />

<strong>in</strong> version <strong>4.5</strong> of .<strong>NET</strong>, help<strong>in</strong>g you overcome the problems that weakly typed collections like the<br />

ArrayList <strong>in</strong>troduced.

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

Saved successfully!

Ooh no, something went wrong!