473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

oo mess: how to make life complicated for yourself with inheritence and collections.please help

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;
}

}
}
}

Feb 8 '07 #1
3 1455
Charlie Bear wrote:
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?
You can't create an object of the base class and cast it to a subclass,
but you can do it the other way around.

Create each object in the switch and assign it to a reference of the
base class. After the switch you can use the reference to set the
properties, regardless of the actual type of the object.

Example:

BaseClass feature;
switch (wantedClass) {
case Class1:
SomeObject.Member1 = new SubClass1();
feature = SomeObject.Member1;
break;
case Class2:
SomeObject.Member2 = new SubClass2();
feature = SomeObject.Member1;
break;
}
feature.Some = 1;
feature.Other = 2;

--
Göran Andersson
_____
http://www.guffa.com
Feb 8 '07 #2
On 8 Feb 2007 12:10:10 -0800, "Charlie Bear" <ch*****@contrapositive.tvwrote:
>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;
}

}
}
}
I may be missing something here, but it seems to me both of your objects share
the same properties except for PollID.

So inheriting two types from Feature you might look at making one inherited
class with all the properties (at this time that appears to be the PollFeature
object) and get rid of the other one. Your already storing the Feature's type
in the Type property, so you can identify a feature's type from that property
when you need to. The designer of the database probably intended this sort of
thing anyway because he/she included the TYPE column.

If you do this you would not need the switch. You would do it like this:

Cve.Core4.Business.TextFeature tf = new FeatureWithoutInheritence();
f.FeatureId = dr.GetInt32(0);
f.Title = dr.GetString(1);
f.FeatureBody = dr.GetString(2);
f.Type = GetInt32(4);
if(f.Type == 2)
{
f.PollID = dr.GetInt32(3);
}
f.ArticleId = dr.GetInt32(5);
f.Slot = dr.GetInt32(6);
f.Filename = dr.GetString(7);
f.Enabled = dr.GetBoolean(8);
f.Order = dr.GetInt32(9);
f.Date = dr.GetDateTime(10);

features.Add(f);

Naturally, since I don't know how you are using the object, what I suggested may
not be appropriate, but it's a way of avoiding the switch statement.

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
Feb 9 '07 #3
Thanks guys. both solutions are viable and useful. My solution to the
problem was a little more complicated (think i might take a simpler
approach):

public static System.Collections.ArrayList
GetFeaturesSql(System.Data.SqlClient.SqlDataReader dr)
{
//create collection object - should use generics really.
System.Collections.ArrayList al = new ArrayList();
if (dr.HasRows)
{
while (dr.Read())
{

//work out the class name...
string className = "Business." +
FeatureController.GetClassNameFromType(Convert.ToI nt32(dr["type"]));
Assembly assembly =
Assembly.GetExecutingAssembly();
AssemblyName assemblyName =
assembly.GetName();
Type t = assembly.GetType(assemblyName.Name + "."
+ className);
//Create the object
object newFeature = Activator.CreateInstance(t);
//get the properties for the type
PropertyInfo[] objPropertiesArray =
t.GetProperties();
//cycle through the data reader columns
for (int i = 0; i < dr.FieldCount - 1; i++)
{
//find the property on the new object
foreach (PropertyInfo p in objPropertiesArray)
{
//if the data reader name and dr name is
the same then get the type and add it (achieved by aliases in the
sproc).
string drName = dr.GetName(i);
if (p.Name == drName)
{
switch (p.PropertyType.ToString())
{

case "System.String":
p.SetValue(newFeature,
dr.GetString(i), null);
break;
case "System.DateTime":
p.SetValue(newFeature,
dr.GetDateTime(i), null);
break;
case "System.Int32":
p.SetValue(newFeature,
dr.GetInt32(i), null);
break;
case "System.Boolean":
p.SetValue(newFeature,
dr.GetBoolean(i), null);
break;
}
}

}
}
// add the new object to the collection.
al.Add(newFeature);
}
}
return al;

}

Feb 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: reynArd | last post by:
"I can teach anyone how to get what they want out of life. The problem is I can't find anybody who can tell me what they want." -- Mark Twain "Today people in America can become whatever they want....
0
by: reynArd | last post by:
"I can teach anyone how to get what they want out of life. The problem is I can't find anybody who can tell me what they want." -- Mark Twain "Today people in America can become whatever they want....
3
by: Gérard Talbot | last post by:
Hello all, When webfonts are used for purely cosmetic/ornemental reasons and on a large scale, I don't agree. When webfonts are used because Unicode support among browsers for a particular...
2
by: bmsgharr | last post by:
On a web site that I am working on I have created a base page called public abstract class WebsiteBasePage : System.Web.UI.Page { ..... } The reason for this is to put standard code in it, for...
3
by: Trammel | last post by:
Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O One of my reasons for upgrading is I was told that VB.net can do class inheritance and subclassing easier. ...
26
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and...
5
by: VMI | last post by:
I understand the principle behind generics, but I'm not sure how I can apply them to a real-life situation. Anybody have an example that shows how someone can benefit from a generic class? The...
24
by: Skijor | last post by:
I have inherited a php script that nests php, javascript, and html. How do I clean up code like this: I think I can move the javascript into a seperate .js file at the least. What about the...
4
by: Giorgio Parmeggiani | last post by:
Hi I am using the svcutil parameter: /ct to obtain IBindingList from WCF Service. I can exclude some collection or some WCF service methods from this conversion? I have some strongly typed...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.