.NET Tip: Creating a Collection of Your Objects Oakland CA

The generic collection introduced with .NET 2.0 allows you to create collections of your objects without having to design a new class.

Local Companies

Propertyware
(415) 455-2400
575 Market St., Ste. 375
San Francisco, CA
International Business Machines Corporation
(415) 545-2000
425 Market St.
San Francisco, CA
ClearKite
(866) 894-8090
355 Berry St., Ste. 240
San Francisco, CA
Speller & Company LLC
415-848-3022
795 Folsom Street
San Francisco, CA
Mindjet Corporation
(415) 229-4200
1160 Battery St., 4th Flr
San Francisco, CA
OpCon Technologies, Inc.
(415) 439-5389
50 California St., Ste. 1500
San Francisco, CA
Science Applications International Corporation (SAIC)
(415) 202-1800
1275 Columbus Ave.
San Francisco, CA
Microsoft Corporation
(415) 972-6400
835 Market St., Ste. 700
San Francisco, CA
San Francisco Technologies, Inc.
(866) 992-2543
123 10th St.
San Francisco, CA
Streetline Networks
(650) 714-5528
995 Market St., 16th Flr.
San Francisco, CA

provided by: 
Originally published at Internet.com


One of the new language features introduced with .NET 2.0 was the generic collection. In the past, even as far back as Visual Basic 6.0, you could create a custom collection class for your classes. With .NET 1.0/1.1, you could add objects to standard collection classes like the ArrayList and the Hashtable classes. However, when you looked at the members of those collections, they were not strongly typed and had to be cast back to the original type.

The generic collection allows you to create collections of your objects without having to design a new class. Take, for example, the ValidationError class I created for a previous tip: public class ValidationError { private string _error; public string ErrorMessage { get { return _error; } set { _error = value; } } public ValidationError(string errorMessage) { ErrorMessage = errorMessage; } }

If you wanted to create a collection of ValidationError objects, you could put them in an ArrayList. However, using a generic collection as shown in the following snippet requires a bit less overhead: List errors = new List(); errors.Add(new ValidationError("Error #1")); errors.Add(new ValidationError("Error #2")); errors.Add(new ValidationError("Error #3")); errors.Add(new ValidationError("Error #4")); foreach (ValidationError err in errors) { Response.Write(err.ErrorMessage + "
"); }

By using the generic List declaration, you create a collection of your custom objects without any extra work. As the snippet shows, you now can loop through the collection and .NET avoids all the extra overhead of converting a generic member of an ArrayList to a ValidationError object.

Several other generic collections are available for other situations. Refer to the help file for more information on this handy new feature.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Propertyware

(415) 455-2400
575 Market St., Ste. 375
San Francisco, CA

Related Local Events
MACWORLD 2010
Dates: 1/4/2010 - 1/8/2010
Location: Moscone Convention Center
San Francisco, CA
View Details

Informex USA
Dates: 2/16/2010 - 2/19/2010
Location: Moscone Convention Center, San Francisco
San Francisco, CA
View Details

Advanced Lithography
Dates: 2/21/2010 - 2/26/2010
Location: San Jose Convention Center
San Jose, CA
View Details

Contraceptive Technology Conference : San Francisco
Dates: 3/24/2010 - 3/27/2010
Location: Hyatt Regency Hotel
San Francisco, CA
View Details

Web 2.0 Summit
Dates: 10/20/2009 - 10/22/2009
Location: Westin San Francisco Market Street
San Francisco, CA
View Details