.NET Tip: Creating a Collection of Your Objects Denver CO

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

Rapid Refill Ink
303-292-9465
1719 Wazee St
Denver, CO
Analytics
303-932-1142
1175 South Cherokee St
Denver, CO
21st Century Technologies, Inc.
303-744-2178
1566 S. Pennsylvania St.
Denver, CO
DENVER DATAMAN
303731-5978
1685 S. Colorado Blvd
Denver, CO
deSabran
(303) 782-9101
5082 E Hampden Ave
Denver, CO
Travel Software Consultants
303-343-9516
1963 Galena St
Aurora, CO
The Logix Group
(720) 259-7500
8801 E. Hampden Ave.
Denver, CO
ORACLE CORPORATION
303334-4000
7604 Technology Way
Denver, CO
ProLine
303-761-3999
P.O. Box 27682
Denver, CO
Mainsale Software LLC
303-337-1155
750 S Fraser St
Aurora, CO

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

Rapid Refill Ink

303-292-9465
1719 Wazee St
Denver, CO
http://www.rapidrefill.com

Related Local Events
SEG - Society of Exploration Geophysicists Annual International Meeting and Exposition 2010
Dates: 10/17/2010 - 10/21/2010
Location: Denver Convention Center
Denver, CO
View Details

INTC 2009
Dates: 9/21/2009 - 9/24/2009
Location: Hyatt Convention Center Hotel
Denver, CO
View Details

RetailVision Fall 2009
Dates: 8/30/2009 - 9/3/2009
Location: Hyatt Regency Denver
Denver, CO
View Details

SANS Rocky Mountain 2009
Dates: 7/7/2009 - 7/13/2009
Location: Grand Hyatt Denver
Denver, CO
View Details

15th International Conference on Solid-State Sensors, Actuators and Microsystems (Transducers 2009)
Dates: 6/21/2009 - 6/25/2009
Location: Sheraton Denver Downtown Hotel
Denver, CO
View Details