i've got myself into a bit of an oo mess. it's probably me
misunderstanding how oo works.
I've got a base class called "Feature" which some classes inherit. in
the database i've stored the data in a table along with a type id to
tell me what type of feature it is. ie a poll feature has an id of two
and has the properties of feature plus a poll id which links to a poll
table. the poll feature has seperate methods that the base feature
doesn't need. hence the need for inheritence.
what i want to do is get the list of features from the database cast
them in to their correct type (by doing a switch on the type id) and
add them to a collection. trouble is i don't want to have to add all
their common properties in the switch as i would be repeating myself
over and over. i can't add the values to the base feautre class then
cast it becuase it doens't work. is there any way i can using generics
or reflection to do what i need? here is an example of what i am doing
at the moment what can i do to make this easier to manage?
private void getFeaturesSql(System.Data.SqlClient.SqlDataReader dr)
{
if(dr.HasRows)
{
while(dr.Read())
{
switch (dr.GetInt32(4))
{
case 1://standard text feature
Cve.Core4.Business.TextFeature tf = new
TextFeature();
tf.FeatureId = dr.GetInt32(0);
tf.Title = dr.GetString(1);
tf.FeatureBody = dr.GetString(2);
tf.Type = 1;
tf.ArticleId = dr.GetInt32(5);
tf.Slot = dr.GetInt32(6);
tf.Filename = dr.GetString(7);
tf.Enabled = dr.GetBoolean(8);
tf.Order = dr.GetInt32(9);
tf.Date = dr.GetDateTime(10);
features.Add(tf);
break;
case 2://poll
Cve.Core4.Business.PollFeature pf = new
PollFeature();
pf.FeatureId = dr.GetInt32(0);
pf.Title = dr.GetString(1);
pf.FeatureBody = dr.GetString(2);
pf.Type = 2;
pf.PollId = dr.GetInt32(3);
pf.ArticleId = dr.GetInt32(5);
pf.Slot = dr.GetInt32(6);
pf.Filename = dr.GetString(7);
pf.Enabled = dr.GetBoolean(8);
pf.Order = dr.GetInt32(9);
pf.Date = dr.GetDateTime(10);
features.Add(pf);
break;
}
}
}
}