Overriding classes in c# and accessing a virtual method which uses an override method

10,443

Solution 1

Your implementation seems correct. It will be correct until you don't override GetReference() method in your DerivedClass.

If you want var c = GetReference(GetName(st)); this line to always call base class, you should take precaution by writing it as base.GetReference(GetName(st))

Solution 2

I think you're on the right track. You'd better use base and this to avoid confusion and force compiler use the implementation you want.

Share:
10,443

Related videos on Youtube

f_olivere
Author by

f_olivere

Updated on July 21, 2022

Comments

  • f_olivere
    f_olivere almost 2 years

    I am working with overriding methods in C# for the first time and I think I have a good grasp of the basics. However my methods are a little more complex and some call methods within the methods.

    I am essentially trying to save myself code writing by finding a way to use the base class method but call the overriden method that the base class method uses.

    Here is an example of my problem, as I might not be explaining it too well:

    namespace exampleCode
    {
        public class BaseClass : AnotherClass
        {
            public static string OrderStorageContainer = "Deliverables";
    
            public virtual string CustomerStorageContainer = "CustomerDocs";
    
            public virtual string GetSomething(typeStorage st, string FileName)
            {
                if (String.IsNullOrWhiteSpace(FileName))
                {
                    throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
                }
    
                var c = GetReference(GetName(st));        
                return c.ToString();
            }
            public virtual string GetName(typeStorage st)
            {
                switch (st)
                {
                    case typeStorage.CustomerProvided:
                        return CustomerStorageContainer.ToLower();
                    case typeStorage.SystemGenerated:
                        return OrderStorageContainer.ToLower();
                }
                throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
            }
    
            public virtual string GetReference()
            {
                //does stuff
            }
        }
    
        public class DervivedClass : BaseClass
        {
            public static string QuoteStorageContainer = "QuoteDeliverables";
    
            public override string CustomerStorageContainer = "QuoteCustomerDocs";
    
            public override string GetSomething(typeStorage st, string FileName)
            {
                if (String.IsNullOrWhiteSpace(FileName))
                {
                    throw new ArgumentNullException("FileName", "The filename of the file should be supplied!");
                }
    
                var c = GetReference(GetName(st));        
                return c.ToString();
            }
    
            public override string GetName(typeStorage st)
            {
                switch (st)
                {
                    case typeStorage.CustomerProvided:
                        return CustomerStorageContainer.ToLower();
                    case typeStorage.SystemGenerated:
                        return QuoteStorageContainer.ToLower();
                }
                throw new InvalidOperationException("No container defined for storage type: " + st.ToString());
            }
    
        }
    }
    

    Basically, I want the Derived class to use the override GetSomething method. However, I want it to call the Base Class GetReference() method with the result from the overriden GetName() method.

    Is this along the right lines? I've been finding it difficult to find a similar exmaple online.

    Any pointers would be great!

    • Alexander Derck
      Alexander Derck about 8 years
      Looks right to me, as long as you don't override the GetReference method you'll call the base one. If you would override it, you can still use base.GetReference().