.NET Tip: Take Advantage of Interfaces Wisconsin

Stop writing more code than needed. You can streamline your code if you use interfaces. You will be able to write simpler, more flexible, and more maintainable applications when you apply interfaces.

Local Companies

R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Kangas Komputing Llc
(715) 453-9803
127 S Tomahawk Ave
Tomahawk, WI
Freedom Data Resources Inc
(715) 831-8118
Eau Claire, WI


.NET Tip: Take Advantage of Interfaces

provided by: 
Originally published at Internet.com


Take advantage of interfaces to make your code cleaner and more reusable. A simple example of where interfaces can be applied is in populating UI controls. How many ListBox controls do you have to populate in your web application? One solution is to write the code to populate each ListBox individually in the Page_Load method. Another method could be to create a helper method that takes an array of data items and a ListBox as parameters, populating the ListBox from the array of data. Here is an example of such a method. private void LoadListBox(object[] data, ListBox ListBoxControl) { for (int i = 0; i < data.Length; i++) ListBoxControl.Items.Add(data[i].ToString()); }

With the LoadListBox method in place, the code to populate the ListBox in the Page_Load method is simplified. string[] TestItems = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; LoadListBox(TestItems, ListBox1);

Although this is better than doing all the work in the Page_Load method, you can do much better. This works fine for a ListBox, but what about other list type controls? If you apply interfaces, you can enhance the helper method to support a variety of data sources and list type controls. Instead of the helper method taking an array of data items and a ListBox control, it now takes an ICollection and a ListControl. private void LoadListControlFromCollection(ICollection data, ListControl ListControlControl) { foreach (object item in data) ListControlControl.Items.Add(item.ToString()); }

Usage of the new LoadListControlFromCollection method to load a ListBox with data items remains the same. string[] TestItems = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; LoadListControlFromCollection(TestItems, ListBox1);

The advantage of the new method, however, is that you also can apply it to other list type controls. The flexibility is evident when you use the same method to populate a CheckListBox, DropDownList, RadioButtonList, or BulletList. LoadListControlFromCollection(TestItems, CheckBoxList1); LoadListControlFromCollection(TestItems, DropDownList1); LoadListControlFromCollection(TestItems, RadioButtonList1); LoadListControlFromCollection(TestItems, BulletedList1);

Applying the use of interfaces can make your applications simpler and more flexible. If you take a look through your code, I bet you can find several places where your application would benefit from using interfaces. Start thinking about interfaces today; the person who comes behind you and has to maintain your code will thank you.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the first beta's release and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

Xorbix Technologies Inc.

414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
http://www.xorbix.com

Regional Articles
- .NET Tip: Take Advantage of Interfaces Appleton WI
- .NET Tip: Take Advantage of Interfaces Baraboo WI
- .NET Tip: Take Advantage of Interfaces Beaver Dam WI
- .NET Tip: Take Advantage of Interfaces Beloit WI
- .NET Tip: Take Advantage of Interfaces Brookfield WI
- .NET Tip: Take Advantage of Interfaces Burlington WI
- .NET Tip: Take Advantage of Interfaces Cedarburg WI
- .NET Tip: Take Advantage of Interfaces Chippewa Falls WI
- .NET Tip: Take Advantage of Interfaces Cudahy WI
- .NET Tip: Take Advantage of Interfaces De Pere WI
- .NET Tip: Take Advantage of Interfaces Delavan WI
- .NET Tip: Take Advantage of Interfaces Eau Claire WI
- .NET Tip: Take Advantage of Interfaces Elkhorn WI
- .NET Tip: Take Advantage of Interfaces Fond Du Lac WI
- .NET Tip: Take Advantage of Interfaces Fort Atkinson WI
- .NET Tip: Take Advantage of Interfaces Franklin WI
- .NET Tip: Take Advantage of Interfaces Green Bay WI
- .NET Tip: Take Advantage of Interfaces Hartland WI
- .NET Tip: Take Advantage of Interfaces Janesville WI
- .NET Tip: Take Advantage of Interfaces Kaukauna WI
- .NET Tip: Take Advantage of Interfaces Kenosha WI
- .NET Tip: Take Advantage of Interfaces La Crosse WI
- .NET Tip: Take Advantage of Interfaces Lake Geneva WI
- .NET Tip: Take Advantage of Interfaces Manitowoc WI
- .NET Tip: Take Advantage of Interfaces Marinette WI
- .NET Tip: Take Advantage of Interfaces Marshfield WI
- .NET Tip: Take Advantage of Interfaces Menasha WI
- .NET Tip: Take Advantage of Interfaces Menomonee Falls WI
- .NET Tip: Take Advantage of Interfaces Menomonie WI
- .NET Tip: Take Advantage of Interfaces Merrill WI
- .NET Tip: Take Advantage of Interfaces Middleton WI
- .NET Tip: Take Advantage of Interfaces Milwaukee WI
- .NET Tip: Take Advantage of Interfaces Mosinee WI
- .NET Tip: Take Advantage of Interfaces Mukwonago WI
- .NET Tip: Take Advantage of Interfaces Muskego WI
- .NET Tip: Take Advantage of Interfaces Neenah WI
- .NET Tip: Take Advantage of Interfaces New Berlin WI
- .NET Tip: Take Advantage of Interfaces Oak Creek WI
- .NET Tip: Take Advantage of Interfaces Oconomowoc WI
- .NET Tip: Take Advantage of Interfaces Onalaska WI
- .NET Tip: Take Advantage of Interfaces Oshkosh WI
- .NET Tip: Take Advantage of Interfaces Pewaukee WI
- .NET Tip: Take Advantage of Interfaces Racine WI
- .NET Tip: Take Advantage of Interfaces Rhinelander WI
- .NET Tip: Take Advantage of Interfaces Rice Lake WI
- .NET Tip: Take Advantage of Interfaces River Falls WI
- .NET Tip: Take Advantage of Interfaces Schofield WI
- .NET Tip: Take Advantage of Interfaces Shawano WI
- .NET Tip: Take Advantage of Interfaces Sheboygan WI
- .NET Tip: Take Advantage of Interfaces South Milwaukee WI
- .NET Tip: Take Advantage of Interfaces Stevens Point WI
- .NET Tip: Take Advantage of Interfaces Sturgeon Bay WI
- .NET Tip: Take Advantage of Interfaces Sun Prairie WI
- .NET Tip: Take Advantage of Interfaces Superior WI
- .NET Tip: Take Advantage of Interfaces Thiensville WI
- .NET Tip: Take Advantage of Interfaces Two Rivers WI
- .NET Tip: Take Advantage of Interfaces Watertown WI
- .NET Tip: Take Advantage of Interfaces Waukesha WI
- .NET Tip: Take Advantage of Interfaces Waupaca WI
- .NET Tip: Take Advantage of Interfaces Wausau WI
- .NET Tip: Take Advantage of Interfaces West Bend WI
- .NET Tip: Take Advantage of Interfaces Whitewater WI
- .NET Tip: Take Advantage of Interfaces 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