Avatar of Peter Pang
Peter Pang asked on

In C#, how to access base class variable from child class?

In C#, how to access base class variable from child class?

e.g.
Base Class
   class TestBase
    {
        protected int a;

        public TestBase(int i)
        {
            a = i;
        }

        protected TestBase()
        {

        }

        public void Update(int i)
        {
            a = i;

            TestChild child = new TestChild();
            child.Update("Hello World ");
        }

    }

ChildClass
   class TestChild:TestBase
    {
        private string msg;
        public void Update (string s)
        {
            msg = s+ a.ToString();
            Console.WriteLine(msg);

        }
    }

Calling
       private void btnTest_Click(object sender, EventArgs e)
        {
            TestBase t = new TestBase(1);
            t.Update(100);

        }

Result
Hello World 0

Problem
I was hoping to get Hello World 100.
How to get child class to access base class variable, in this case int a ?
.NET ProgrammingC#

Avatar of undefined
Last Comment
AndyAinscow

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
AndyAinscow

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Kyle Abrahams

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Francis Empleo

Actually you can't. The reason is obvious because it is a protected property. You can access this by creating a public method inside you base class and call it from your child class
it_saige

There is no need to create a public method in the base/parent class.  The child *can* update a protected variable since the child inherited the parent (or derived from the parent).
protected: Access is limited to the containing class or types derived from the containing class.


- Source
Another proof of concept.
using System;

namespace EE_Q29097909
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = new TestBase(1);
            t.Update(100);
            Console.ReadLine();
        }
    }

    class TestBase
    {
        protected int a;

        public TestBase(int i)
        {
            a = i;
        }

        protected TestBase() {; }

        public void Update(int i)
        {
            a = i;

            var child = new TestChild();
            Console.WriteLine($"Base class a = {a}; Child a = {child.a}");
            child.a = a;
            Console.WriteLine($"Base class a = {a}; Child a = {child.a}");
            child.Update("Hello World");
        }
    }

    class TestChild : TestBase
    {
        public void Update(string s)
        {
            Console.WriteLine($"{s} {a}");
        }
    }
}

Open in new window

Which produces the following output -Capture.PNGIt is as Kyle stated, the OP instantiates a new Child class but does not give the child class instance the updated value for a.  Assigning the child's 'a' from the base update method is one possible solution.

-saige-
Francis Empleo

Oh yeah youre right. Sorry forgot about the inheritance. Thanka for correcting :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Kyle Abrahams

Also just a note that with reflection the protection model goes away.  I've been able to update private variables in a 3rd party DLL when needed.  Not saying that you *should* do it, and if you do do it you need to understand all the impacts of said change, but you *CAN* do it.
AndyAinscow

re inheritance - see the code in the very first comment.
AndyAinscow

The code in the question didn't use a parent-child relationship.  Hence it didn't work.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.