Re: oo mess: how to make life complicated for yourself with inheritence and collections.please help
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Thu, 08 Feb 2007 22:30:03 +0100
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
.
- References:
- Prev by Date: Re: Office 2007 "Feel"
- Next by Date: Re: multi-threaded app/ using lock
- Previous by thread: oo mess: how to make life complicated for yourself with inheritence and collections.please help
- Next by thread: Re: oo mess: how to make life complicated for yourself with inheritence and collections.please help
- Index(es):
Relevant Pages
|