Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:17:54+00:00 2026-06-04T07:17:54+00:00

I am trying to achieve calling methods of the Base class and the Derived

  • 0

I am trying to achieve calling methods of the Base class and the Derived class. However, I am a little confused if I am doing it correctly. I would like to set values from the Base class and use them in the Derived class.

namespace Inheritance
{
    using System;

    public class BaseClass
    {
        public BaseClass() { }

        protected string methodName;
        protected int noOfTimes;
        public void Execute(string MethodName, int NoOfTimes)
        {
            this.methodName = MethodName;
            this.noOfTimes = NoOfTimes;
        }
    }

    public class DerivedClass : BaseClass
    {
        public DerivedClass() : base() { }

        public void Execute()
        {
            Console.WriteLine("Running {0}, {1} times", base.methodName, base.noOfTimes);
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            DerivedClass d = new DerivedClass();
            d.Execute("Func", 2);
            d.Execute();

            Console.ReadLine();
        }
    }
}

Question: can I achieve the same as above using only 1 call to Execute instead of 2?

I hope my example above is clear. Please let me know if its otherwise and I will provide additional details.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:17:55+00:00Added an answer on June 4, 2026 at 7:17 am

    Disclaimer, my answer assumes you wanted a solution revolving around inheritance of the method and it’s implementation. Other answers provided are also good solutions to the question.

    It sounds like you want to override a method in a derived class. To do this, the base class needs to mark the method virtual or abstract. However, in your case the derived class method signature is different. I will assume that this is an error in not understanding how it would work, so I have corrected it for you.

    In this instance, the derived class marks the method with override, a C# keyword. I then call base.Execute(MethodName, NoOfTimes); as I want to use the base class’s implementation in my overridden implementation. I then put in my own code.

    base is another keyword that lets you access members in the base class immediately below the class you are in without having the inheritance chain push you back into derived members.

    abstract is worth reading up on, but is not required in this case (it has different behaviour to virtual but also gets used in conjunction with override).

    namespace Inheritance
    {
        using System;
    
        public class Program
        {
            internal protected class BaseClass
            {
                public BaseClass() { }
    
                protected string methodName;
                protected int noOfTimes;
                public virtual void Execute(string MethodName, int NoOfTimes)
                {
                    this.methodName = MethodName;
                    this.noOfTimes = NoOfTimes;
                }
            }
    
            internal class DerivedClass : BaseClass
            {
                public DerivedClass() : base() { }
    
                public override void Execute(string MethodName, int NoOfTimes)
                {
                    base.Execute(MethodName, NoOfTimes);
                    Console.WriteLine("Running {0}, {1} times", base.methodName, base.noOfTimes);
                }
            }
    
            static void Main(string[] args)
            {
                DerivedClass d = new DerivedClass();
                d.Execute("Func", 2);
    
                Console.ReadLine();
            }
        }
    }
    

    There are a few code formatting and naming quirks with this sample, but I will leave this alone to stay as close to your original code as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am trying to achieve is something like this: class object: def __init__(self):
im trying to achieve something but i dont really know how I have set
Trying to achieve something like this using recursion: if (m > n) return; Foo
Trying to achieve any moving effect while appending an element from one to another
I am trying to achieve reading variables from a Java applet out of process
I'm trying to achieve an effect similar with youtube home page (you must be
I am trying to achieve an LDAP query to gather all properties we have
I am trying to achieve the following: <Style TargetType=ListBoxItem> <Setter Property=ContextMenu> <Setter.Value> <ContextMenu> <MenuItem
What I'm trying to achieve: Intercept requests for .asp files using an asp.net application
What I am trying to achieve is for the application icon to be different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.