hi,
i am not sure where to post this, so i will post it here and wait to be directed. i have a problem with inheritance which leads me to believe that i am doing something fundamentally wrong.
i have 2 classes:
public class division
{
public string divisionName;
}
public class team
{
public string teamName;
public division division;
}
these 2 classes are base. if i inherit both classes:
public class superDivision : division {}
public class superTeam : team{}
i face the problem that superTeam.division returns me a base.division object. doesn't seem too bad, but the problem comes when i store a list of teams in the base.division. these teams are the base.team. so when i access them through superTeam i get a list of base.team.
This makes me start to use the "new" operator on superTeam to return the list in the type i want, namely superTeam. This starts getting mental at this point.
Am i missing a pattern is it right to downcast/upcast the object into the type i want what should i do
Hope you can help and won't be too brutal in your replies...
kode monkey

class inheritance
cwlaualex
thanks for your replies. I found them useful.
In the end i decided to create an interface that my classes would use and return a list<interface>. This makes it works in a "neater" way. i think.
Thanks again all.
Marc Jones
barkingdog
Can-Ann
You're thinking that given b:a, that list<b>:list<a>. Unfortunately, C# doesn't allow this.
http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx
bluejay_2006
thanks for that, reassurance is as good as an answer ;)
i feel more confident now :)
HPEvju
absolutely.
i have these objects, division and team. they are used throughout different applications. however, in these different applications the methods that will be used by these objects will be different, depending on the application.
so i thought i would create a base division and base team class and inherit them in the appropriate applications and give them the methods they need for the application.
I could always give ALL the methods to the base class and pass interfaces to the applications. but i just wondered if this scenario had a "fix".
the problem arises from the fact that i have 2 base classes that use each other and as i extend them, they return the base version rather than the inherited version.
baseTeam returns property of type baseDivision - i'm ok with this
superTeam returns property of type baseDivision - i'd rather it return superDivision
i use the "base" constructors, but because the protected field for storing the division is in the baseDivision type, and the property was defined in the baseTeam, it always returns baseDivision.
this may mean that i have to use the "new" operator or i could cast them but it just looks messy.
hope this helps explain better....