c# inheritance trickery with a hierachy

127 Views Asked by At

I have a hiearchy of organizations. Basically, they all share a common base and then each class will declare a name and id. Every derived class will pick up the previous name and id.

I need the derived class to be able to show the inherited class's Name property. I'm trying to use base.Name in the get accessor.. Doesnt seem to work.

I'm using NHibernate so the properties have to be virtual.. This may be a problem.

I have tried to "hide" the Name property by using "new" but then I think base.Name is the new version.

The hiearchy looks like this...

    public class Base 
    {
       public virtual string Name {get;set;}
       public virtual string Website {get;set;}
    }
    public class Jurisdiction : Base
    {
       public virtual string JurisdictionId {get;set;}
    }
    public class Conference : Jurisdiction
    {
       public virtual string JurisdictionName {get{ return base.Name; }}
       public virtual string ConferenceId {get;set;}
    }
    public class District : Conference
    {
       public virtual string ConferenceName {get {return base.Name; }}
       public virtual string DistrictId {get;set;}
    }

I need..

  • District.ConferenceName to return Conference.Name
  • Conference.JurisdictionName to return Jurisdiction.Name

I have googled and stackoverflowed and cannot find the answers.. Thanks for the help

EDIT for clarity:

I need the classes to to look like this.

  • District -> Name, DistrictId, ConferenceId, ConferenceName, JurisdictionId, JurisidictionName
  • Conference -> Name, ConferenceId, JurisdictionId, JurisdictionName
  • Jurisdiction -> Name, JurisdictionId
1

There are 1 best solutions below

4
On BEST ANSWER

I believe your issue is that you may be confused about how the inheritance is working. The Name property that you are using to identify ConferenceName will be the same as the name of the District.

You need a class structure where you either include an instance of a Conference class in District or you copy the ConferenceName into a property of District without referencing base.

Update to reflect comment discussion:

 public class District : Conference
    {
       public virtual string ConferenceName {get;set;}
       public virtual string DistrictId {get;set;}
    }