473,659 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

i've got myself into a bit of an oo mess. it's probably me
misunderstandin g 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.Sql Client.SqlDataR eader dr)
{
if(dr.HasRows)
{
while(dr.Read() )
{
switch (dr.GetInt32(4) )
{
case 1://standard text feature
Cve.Core4.Busin ess.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.Busin ess.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 1476
Charlie Bear wrote:
i've got myself into a bit of an oo mess. it's probably me
misunderstandin g 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.Memb er1 = new SubClass1();
feature = SomeObject.Memb er1;
break;
case Class2:
SomeObject.Memb er2 = new SubClass2();
feature = SomeObject.Memb er1;
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*****@contra positive.tvwrot e:
>i've got myself into a bit of an oo mess. it's probably me
misunderstandi ng 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.Sql Client.SqlDataR eader dr)
{
if(dr.HasRows)
{
while(dr.Read() )
{
switch (dr.GetInt32(4) )
{
case 1://standard text feature
Cve.Core4.Busin ess.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.Busin ess.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.Busin ess.TextFeature tf = new FeatureWithoutI nheritence();
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.Collecti ons.ArrayList
GetFeaturesSql( System.Data.Sql Client.SqlDataR eader dr)
{
//create collection object - should use generics really.
System.Collecti ons.ArrayList al = new ArrayList();
if (dr.HasRows)
{
while (dr.Read())
{

//work out the class name...
string className = "Business." +
FeatureControll er.GetClassName FromType(Conver t.ToInt32(dr["type"]));
Assembly assembly =
Assembly.GetExe cutingAssembly( );
AssemblyName assemblyName =
assembly.GetNam e();
Type t = assembly.GetTyp e(assemblyName. Name + "."
+ className);
//Create the object
object newFeature = Activator.Creat eInstance(t);
//get the properties for the type
PropertyInfo[] objPropertiesAr ray =
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 objPropertiesAr ray)
{
//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(newF eature,
dr.GetString(i) , null);
break;
case "System.DateTim e":
p.SetValue(newF eature,
dr.GetDateTime( i), null);
break;
case "System.Int 32":
p.SetValue(newF eature,
dr.GetInt32(i), null);
break;
case "System.Boolean ":
p.SetValue(newF eature,
dr.GetBoolean(i ), null);
break;
}
}

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

}

Feb 9 '07 #4

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

Similar topics

0
1517
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. Trouble is most don't know what they want." -- Earl Nightingale What do you want? Are you sick of the rat race? Are you tired of having someone else tell you what they think you're worth? Would you rather work from home, set you own hours, and pay...
0
1345
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. Trouble is most don't know what they want." -- Earl Nightingale What do you want? Are you sick of the rat race? Are you tired of having someone else tell you what they think you're worth? Would you rather work from home, set you own hours, and pay...
3
2495
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 language (like a Canadian Aboriginal language) is inexistent (see http://www.alanwood.net/unicode/unified_canadian_aboriginal_syllabics.html ),
2
1094
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 example retrieving application settings from web.config, but each time I try to open a decendant page
3
1934
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. Would someone be so kind as to provide a small demo about classes for some
26
2161
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 "points" Now, I also have a table called all_matches. This table contains every match report. Over 25,000 of them. I have a "username" field an "outcome" field an "username1" field and "extra_match" field.
5
5449
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 less complicated it is, the better :-) Thanks.
24
1528
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 nesting of php inside the html? <?php ....snip... function ShowCart()
4
1757
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 custom collections, which are serializable, and I want, sometime, to use my custom collection, not IBindingList. Thank in advance
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.