.NET Tip: Create a Class with Overloaded Constructors

When I create a class, I often define a constructor to ensure that the object is initially populated with all the information it needs to operate properly. As an example, one of my databound objects can take a database connection or both a database connection and an object representing the unique ID for the record. Each of these methods for instantiating the object is a separate constructor method. This tip teaches you how to overload the constructor of a class, which is the method called when you instantiate an object.

To see how this works, take a simple class (DataObject) that has three public variables on it:

public class DataObject
{
   public string Value1;
   public string Value2;
   public string Value3;

   public DataObject(string value1, string value2, string value3)
   {
      Value1 = value1;
      Value2 = value2;
      Value3 = value3;
   }

   public DataObject(DataRow inputRow)
   {
      Value1 = inputRow["value1"].ToString();
      Value2 = inputRow["value2"].ToString();
      Value3 = inputRow["value3"].ToString();
   }
}

One of the class's constructors will accept values for each of the three variables, and its other constructor will accept a DataRow that has values for each of the three variables. The code assumes that you have a database table somewhere that has fields named value1, value2, and value3, and that all three fields are strings.

To call this function, you can use one of the following methods, which will end up populating the DataObject class with the three values:

DataObject obj1 = new DataObject("test1", "test2", "test3");

DataTable dt = <code to retrieve a data table>
DataObject obj2 = new DataObject(dt.Rows[0]);

You also can use this method to overload any method on your class. As long as the parameter list is different, you can add an additional overload. You can't, for instance, have two overloads that each has three string parameters. You can have one overload that accepts one string and another that accepts two strings.

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.



Comments

  • There are no comments yet. Be the first to comment!

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

Top White Papers and Webcasts

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds