.NET Tip: Creating a Collection of Your Objects Cincinnati OH

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

3D Computer Systems
513-569-0500
2230 Auburn Ave
Cincinnati, OH
The Shepherd Chemical Co.
(513) 731-1110
4900 Beech Street
Norwood, OH
A-Hi Computers & Electronics
(513) 531-1111
4030 Montgomery Road
Cincinnati, OH
Netburg Consulting Services
513-247-9701
150 E 4th Street
Cincinnati, OH
Graphic Informatn Systems Inc
513-948-1300
7633 Production Dr
Cincinnati, OH
21st Century Designs Inc.
(513) 662-5862
P.O. Box 112091
Cincinnati, OH
Greater Cincinnati Computer
513-985-1300
7024 Plainfield Rd
Cincinnati, OH
Cincinnati PC Solutions
513-871-3600
4770 Redbank Expressway
Cincinnati, OH
Chemical Systems International Inc.
(513) 385-6688
3567 Blue Rock Road
Cincinnati, OH
MIKAL Salon and Spa Computers
513-528-5100
4382 Mt Carmel Tobasco Rd
Cincinnati, OH

 

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

3D Computer Systems

513-569-0500
2230 Auburn Ave
Cincinnati, OH

Related Local Event
Forge Fair 2010
Dates: 4/20/2010 - 4/22/2010
Location: Cincinnati Convention Center
Cincinnati, OH
View Details