Let's say I've got an object Customer
with a couple properties (ID
, FirstName
, LastName
). I've got the default constructor Customer()
, but then I've also got a Customer(DataRow dr)
, since I load this object from a database and that's a simple way to do it.
I frequently come to a point where I want to set up another constructor, Customer(int ID)
, for times when I want to load a Customer
but I haven't made the trip to the database yet. The simplest way to me seems to be like so:
Customer(int ID)
{
DataTable dt = DataAccess.GetCustomer(ID);
if (dt.Rows.Count > 0)
{
// pass control to the DataRow constructor at this point?
}
else
{
// pass control to the default constructor at this point?
}
}
It makes sense to reuse the code that's already in the DataRow constructor, but I can't figure out a way to call that and return what it gives me. Through Googling, I've found information about constructor overloading with the : this()
syntax, but all those examples seem backwards or incompatible with what I'm trying to do.
So there's a gap in my understanding of constructors, but I can't seem to sort it out. What am I missing?