I ran into a problem that I don’t know how to solve, and I hope that the community can help.
I am writing an application that manages Lead objects. (These are ad campaigns.) One part of my program will import entries from a text file. Now the text file contains many potential customers, some of which I want to import, and some of which I will not.
To simplify programming (and use), I parse a text file into a List <Lead> object and use a DataGridView to display potential clients by setting the DataSource DataGridView property.
What I want to do is add a column to the grid called “Import” with a checkbox that the user can check to indicate whether to import each output.
My first thought is to derive a class from Lead:
public Class LeadWithImportCheckbox : Lead { bool bImport = false;
public bool Import {get {return bImport;} set {bImport = value;}}}
However, the parsing mechanism returns a list of Lead objects. I cannot omit Lead to LeadWithImportCheckbox. This fails:
LeadWithImportCheckbox newLead = (LeadWithImportCheckbox)LeadFromParsingEngine;
This is an invalid cast.
Another option I see is creating a constructor for LeadWithImportCheckbox:
public LeadWithImportCheckbox(Lead newlead) { base.Property1 = newlead.Property1; base.Property2 = newlead.Property2; .... base.Property_n = newlead.Property_n; }
This is problematic for two reasons. First, the Lead object has several dozen properties, and writing this constructor is PITA.
But worse, if I ever change the basic structure of Lead, I need to remember to return and change this constructor for LeadWithImportCheckbox. This is dangerous for my code maintenance.
Is there a better way to achieve my goal?
inheritance c # downcasting
The demigeek
source share