How to call a method in a base class from a class that inherits a class that inherits the base class

Here is what I am trying to do:

public class BaseClass
{
     public virtual void SomeMethod()
     {
            // do some stuff here
     }
}

public class DerivedA : BaseClass
{
     public override void SomeMethod()
     {
            PrivateMethod();
     }

     private void PrivateMethod()
     {
            base.SomeMethod();
      }
}

public class DerivedB : DerivedA
{
     public override SomeMethod()
     {
           AnotherPrivateMethod();
     }

      private AnotherPrivateMethod()
      {
            base.SomeMethod();                         // HERE IS WHERE MY PROBLEM IS.  I NEED TO CALL SomeMethod in BaseClass
       }
}

Open in new window


The problem I have is I need to call SomeMethod that is in BaseClass when called within DerivedB but it is calling SomeMethod in DerivedA.  How do I call SomeMethod in BaseClass?  base keyword is only allowing me to access DerivedA class.
dyaroshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
// HERE IS WHERE MY PROBLEM IS.  I NEED TO CALL SomeMethod in BaseClass

Open in new window


In short words: No, you don't. Cause you're using inheritance for some good reasons. This is basically the reason why you cannot do it in C#.

Please do yourself a favor and describe why you need to call this method and why you want to ignore your DerivedA class.

There is basically only one solution, when you really need to do this: You need a different inheritance structure.

btw, can you post a concise, complete and concrete example?
dyaroshAuthor Commented:
    public class ReferralEntity : IReferralEntity
    {
        public virtual object PopulateReferralViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            var model = new ReferralViewModel
            {
                Referral = new ReferralViewInfo
                {
                    ReferralId = referral.ReferralsId,
                    ReferralTypeId = referral.ReferralType.ReferralTypeId,
                    ReferralTypeName = referral.ReferralType.ReferralTypeName,
                    ReferralGroupId = referral.ReferralGroup.ReferralGroupId,
                    ReferralGroupName = referral.ReferralGroup.ReferralGroupName,
                    ReferralGuideLines = referral.ReferralType.ReferralTypeGeneralGuidelines,
                    ReferralSynopsis = referral.ReferralsSynopsis,
                    ReferralPriority = referral.ReferralPriority,
                    EstimatedCompletionDate = referral.EstimatedCompletionDate,
                    ReferralController = referral.ReferralType.ReferralTypeRoute,
                    Comments = referral.ReferralComments,
                    ReferralStatuses = referral.ReferralStatuses
                },
                Buttons = GetReferralViewButtions(referral, emprec)
            };
            return model;
        }
}

    public class PAReferralEntity : ReferralEntity
    {
        public override object PopulateReferralViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
                return GetACHPaymentViewModel(referral, emprec);
        }

        private PremiumAccountingACHPaymentViewModel GetACHPaymentViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            return new PremiumAccountingACHPaymentViewModel
            {
                CommonReferralData = base.PopulateReferralViewModel(referral, emprec) as ReferralViewModel,
                CommonPremiumAccountingData = PopulateCommonPolicyViewInfo(referral),
                ACHPaymentData = PopulateACHPaymentViewInfo(referral, emprec)
            };
        }
}

    public class PAARReferralEntity : PAReferralEntity
    {
        public override object PopulateReferralViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            if (referralTypeId == _COMMISSION_INQUIRIES_ID)
                return GetCommissionInquiresViewModel(referral, emprec);
            return base.GetViewReferralViewNameForReferralType(referralTypeId);
        } 

        private object GetCommissionInquiresViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            return new PremiumAccountingCommissionInquiresViewModel
            {
                CommonReferralData = base.PopulateReferralViewModel(referral, emprec) as ReferralViewModel,
                CommissionInquiresData = new CommissionInquiresViewInfo
                {
                    AgentID = referral.PremiumAccounting.PAPolicyNumber,
                    AgentName = referral.PremiumAccounting.PAName,
                }
            };
        }
}

Open in new window


In the GetCommissionInquiriesViewModel method a call is made to base.PopulateReferralViewModel which calls the PopulateReferralViewModel in PAReferralEntity.  I need the PopulateReferralViewModel in ReferralEntity called.

The ReferralEntity class implements an interface.  PAReferralEntity is a type of ReferralEntity.  PAReferralEntity derives from ReferralEntity and only overrides the methods that return the ViewModels.  Since every view model contains properties from ReferralEntity, the call to base PopulateReferralViewModel is used to set those properties.

PAARReferralEntity is a type of PAReferralEntity which adds an additional PAReferralEntity so the PAARReferralEntity derives from PAReferralEntity and overrides the PopulateReferralViewModel to check if the new referral type needs to be returned.  If not, a call is made to the PopulateReferralViewModel in the PAReferralEntity class.

So the reason I need to access the PopulateReferralViewModel method from the PAARReferralEntity is to be able to populate the ReferralEntity properties.

Right now I solved the problem by making the base.PopulateReferralViewModel(referral, emprec) as ReferralViewModel an internal method in PAReferralEntity and I call that method when trying to set the ReferralEntity properties.

        internal ReferralViewModel CallBasePopulateReferralViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            return base.PopulateReferralViewModel(referral, emprec) as ReferralViewModel;
        }

        private PremiumAccountingACHPaymentViewModel GetACHPaymentViewModel(Referrals referral, EMP_EMPLOYEE emprec)
        {
            return new PremiumAccountingACHPaymentViewModel
            {
                CommonReferralData = CallBasePopulateReferralViewModel(referral, emprec) as ReferralViewModel,
                CommonPremiumAccountingData = PopulateCommonPolicyViewInfo(referral),
                ACHPaymentData = PopulateACHPaymentViewInfo(referral, emprec)
            };
        }

Open in new window


I hope this helps to clarify my question.  I'm not sure if my solution is the best option.
ste5anSenior DeveloperCommented:
Without further context, the problem is best described by

if (referralTypeId == _COMMISSION_INQUIRIES_ID)
                return GetCommissionInquiresViewModel(referral, emprec);

Open in new window


where you try to overcome that restriction. This kind of switch should definitely be covered by it's own types.

Take a look at this really great video from Misko Hevery: "The Clean Code Talks -- Inheritance, Polymorphism, & Testing"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
I am going to implement a factory that will return the correct referral entity based on the referral type.  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.