.NET Tip: Creating a Collection of Your Objects Wisconsin

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

R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Becker Technology Inc
(262) 628-3531
3051 Beechwood Industrial
Hubertus, WI
Larson Consulting
(920) 346-5876
127 E Main St
Brandon, WI


.NET Tip: Creating a Collection of Your Objects

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

Acumium

608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
http://www.acumium.com

Regional Articles
- .NET Tip: Creating a Collection of Your Objects Appleton WI
- .NET Tip: Creating a Collection of Your Objects Baraboo WI
- .NET Tip: Creating a Collection of Your Objects Beaver Dam WI
- .NET Tip: Creating a Collection of Your Objects Beloit WI
- .NET Tip: Creating a Collection of Your Objects Brookfield WI
- .NET Tip: Creating a Collection of Your Objects Burlington WI
- .NET Tip: Creating a Collection of Your Objects Cedarburg WI
- .NET Tip: Creating a Collection of Your Objects Chippewa Falls WI
- .NET Tip: Creating a Collection of Your Objects Cudahy WI
- .NET Tip: Creating a Collection of Your Objects De Pere WI
- .NET Tip: Creating a Collection of Your Objects Delavan WI
- .NET Tip: Creating a Collection of Your Objects Eau Claire WI
- .NET Tip: Creating a Collection of Your Objects Elkhorn WI
- .NET Tip: Creating a Collection of Your Objects Fond Du Lac WI
- .NET Tip: Creating a Collection of Your Objects Fort Atkinson WI
- .NET Tip: Creating a Collection of Your Objects Franklin WI
- .NET Tip: Creating a Collection of Your Objects Green Bay WI
- .NET Tip: Creating a Collection of Your Objects Hartland WI
- .NET Tip: Creating a Collection of Your Objects Janesville WI
- .NET Tip: Creating a Collection of Your Objects Kaukauna WI
- .NET Tip: Creating a Collection of Your Objects Kenosha WI
- .NET Tip: Creating a Collection of Your Objects La Crosse WI
- .NET Tip: Creating a Collection of Your Objects Lake Geneva WI
- .NET Tip: Creating a Collection of Your Objects Manitowoc WI
- .NET Tip: Creating a Collection of Your Objects Marinette WI
- .NET Tip: Creating a Collection of Your Objects Marshfield WI
- .NET Tip: Creating a Collection of Your Objects Menasha WI
- .NET Tip: Creating a Collection of Your Objects Menomonee Falls WI
- .NET Tip: Creating a Collection of Your Objects Menomonie WI
- .NET Tip: Creating a Collection of Your Objects Merrill WI
- .NET Tip: Creating a Collection of Your Objects Middleton WI
- .NET Tip: Creating a Collection of Your Objects Milwaukee WI
- .NET Tip: Creating a Collection of Your Objects Mosinee WI
- .NET Tip: Creating a Collection of Your Objects Mukwonago WI
- .NET Tip: Creating a Collection of Your Objects Muskego WI
- .NET Tip: Creating a Collection of Your Objects Neenah WI
- .NET Tip: Creating a Collection of Your Objects New Berlin WI
- .NET Tip: Creating a Collection of Your Objects Oak Creek WI
- .NET Tip: Creating a Collection of Your Objects Oconomowoc WI
- .NET Tip: Creating a Collection of Your Objects Onalaska WI
- .NET Tip: Creating a Collection of Your Objects Oshkosh WI
- .NET Tip: Creating a Collection of Your Objects Pewaukee WI
- .NET Tip: Creating a Collection of Your Objects Racine WI
- .NET Tip: Creating a Collection of Your Objects Rhinelander WI
- .NET Tip: Creating a Collection of Your Objects Rice Lake WI
- .NET Tip: Creating a Collection of Your Objects River Falls WI
- .NET Tip: Creating a Collection of Your Objects Schofield WI
- .NET Tip: Creating a Collection of Your Objects Shawano WI
- .NET Tip: Creating a Collection of Your Objects Sheboygan WI
- .NET Tip: Creating a Collection of Your Objects South Milwaukee WI
- .NET Tip: Creating a Collection of Your Objects Stevens Point WI
- .NET Tip: Creating a Collection of Your Objects Sturgeon Bay WI
- .NET Tip: Creating a Collection of Your Objects Sun Prairie WI
- .NET Tip: Creating a Collection of Your Objects Superior WI
- .NET Tip: Creating a Collection of Your Objects Thiensville WI
- .NET Tip: Creating a Collection of Your Objects Two Rivers WI
- .NET Tip: Creating a Collection of Your Objects Watertown WI
- .NET Tip: Creating a Collection of Your Objects Waukesha WI
- .NET Tip: Creating a Collection of Your Objects Waupaca WI
- .NET Tip: Creating a Collection of Your Objects Wausau WI
- .NET Tip: Creating a Collection of Your Objects West Bend WI
- .NET Tip: Creating a Collection of Your Objects Whitewater WI
- .NET Tip: Creating a Collection of Your Objects Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Industrial Goods & Services Software
Business Services Family Insurance Technology
Career Financial Services Internet Telecommunications
Cars Food & Beverage Legal Transportation & Logistics
Computer Hardware Health Real Estate Travel
Construction Home Services Retail & Consumer Services Wedding
Education