.NET Tip: Create a Class with Overloaded Constructors Honolulu HI

Overloading the constructor of a class, which is the method called when you instantiate an object, ensures that your object is populated with all the information it needs to operate properly.

Local Companies

Laser Barcode Solutions
(808) 591-0031
1314 S King St Suite 424
Honolulu, HI
Success Solutions LLC
(808) 946-0005
1311 Kapiolani Blvd
Honolulu, HI
Century Computers Inc
(808) 585-0444
500 Ala Moana Blvd
Honolulu, HI
City and County Offices Human Resources Department
(808) 523-4121
650 S King St
Honolulu, HI
Compusultants Inc
(808) 545-1216
1188 Bishop St
Honolulu, HI
Computer 1 Associates Inc
(808) 524-6700
1188 Bishop St
Honolulu, HI
Computer Associates International Inc
(808) 585-0057
1132 Bishop St
Honolulu, HI
Human Touch Software Co
(808) 924-3930
2222 Kalakaua Ave
Honolulu, HI
Analyzer USA Inc
(808) 922-4620
2155 Kalakaua Ave
Honolulu, HI
Retail Solutions Inc
(808) 834-1100
2828 Paa St
Honolulu, HI

provided by: 
Originally published at Internet.com


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 = 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.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Laser Barcode Solutions

(808) 591-0031
1314 S King St Suite 424
Honolulu, HI

Related Local Events
GLOBECOM 2009 - 2009 IEEE Global Telecommunications Conference
Dates: 11/30/2009 - 12/4/2009
Location: Hilton Hawaiian Village
Honolulu, HI
View Details

2009 IEEE International Professional Communication Conference (IPCC 2009)
Dates: 7/19/2009 - 7/22/2009
Location: Hilton Hawaiian Village
Honolulu, HI
View Details

Plant Biology 2009
Dates: 7/18/2009 - 7/22/2009
Location: Hawaii Convention Center
Honolulu, HI
View Details

Plant Biology 2009
Dates: 7/18/2009 - 7/22/2009
Location: Honolulu Convention Center
Honolulu, HI
View Details