.NET Tip: Take Advantage of Interfaces

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.



Comments

  • Interface

    Posted by abc on 07/11/2012 08:25am

    Interface like a class because it support methods,events,properties. but unlike class because it cannot support implemetation..main thing is multiple interfaces are implement at the same time..It display the details based on the user needs other data's are hided..so it can be reduce the complexity of the application..

    Reply
  • what kind of interface?

    Posted by dotnetprof on 07/21/2007 03:11am

    I'm not able to gather what kind of interface you tryin' to explain here. You didn't declare any interface here and not implemented it. Are really talkin' bout C# interfaces or some other conceptual thing?

    • Using built in interfaces, not creating your own

      Posted by jaydmiller on 07/23/2007 07:16am

      I am explaining the use of interfaces in parameters. Instead of declaring the first parameter to the function as an array of objects, it is declared as an ICollection. In the second parameter I show the benefit of using an abstract base class for the parameter type. Using a base class as a parameter type provides the same type of advantages as using an interface in the first parameter.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds