I have the following to classes(POCO’s?) where the second is supposed to extend the first.
I am guessing I need a property referencing the base instance, Business in this case, but do I also need a Constructor to “fill” that property since an Insurance Company can’t exist without a Business?
My reference for this is the underlying DB where tblInsuranceCompany has a FK constraint on column BusinessID back to tblBusiness.
Additionally, I am very new at OOP so please point out anything else you may see “wrong”. Thanks,
public class Business
{
public int BusinessID { get; set; }
public BusinessType BusinessType { get; set; }
public string Name { get; set; }
public string ContactName { get; set; }
public string EmailAddress { get; set; }
public DateTime? InactiveDate { get; set; }
public IList<Address> Addresses { get; set; }
public IList<Phone> Phones { get; set; }
public string DisplayString { get { return this.ToString(); } }
public override string ToString()
{
return String.Format("{0}: {1}", Name, BusinessType.TypeDescription);
}
}
public class InsuranceCompany : Business
{
public int InsuranceCompanyID { get; set; }
public Business Business { get; set; }
public InsuranceCompanyType InsuranceCompanyType { get; set; }
public string DRIInsuranceCompanyNumber { get; set; }
public string DisplayString { get { return this.ToString(); } }
public override string ToString()
{
return String.Format("{0}: {1}", Business.Name, InsuranceCompanyType.TypeDescription);
}
}
No, you don’t need this. The InsuranceCompany is the business. It will also have all of the properties of
Businesssince it inherits fromBusiness. You don’t need a property to reference this, as the reference to theInsuranceCompanyobject will be the same reference as the reference to the “business” portion (they’re the same object).All classes get a constructor – When you construct an InsuranceCompany, it will effectively construct the “business” as well.
That being said, you may want a constructor that passes the relevant information to the base class constructor – but if you don’t provide one, the compiler will create a default constructor for you.