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 ?
Peter PangAsked:
Who is Participating?
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.

AndyAinscowFreelance programmer / ConsultantCommented:
>>How to get child class to access base class variable, in this case int a ?

Your problem is that you are NOT wanting to access a variable in the base class in your example.  In fact it isn't clear just what you want to achieve in your example.

I think this is what you wanted

   class TestBase
    {
        protected int a;

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

        protected TestBase()
        {

        }

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


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

        }
    }


        private void btnTest_Click(object sender, EventArgs e)
        {
            TestChild t = new TestChild(1);
            t.Update(100);
t.Update("Hello world");
        }

Open in new window

0

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
Kyle AbrahamsSenior .Net DeveloperCommented:
The issue is you're creating a new instance of the child class.

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

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

Open in new window


If you can't assign a because it's protected you can expose an update method or use reflection to assign the variable.
0
it_saigeDeveloperCommented:
Proof of concept for Kyle's explanation -
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.Update("Hello World", i);
        }
    }

    class TestChild : TestBase
    {
        string msg;

        public void Update(string s, int i)
        {
            a = i;
            msg = $"{s} {a}";
            Console.WriteLine(msg);
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG-saige-
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Francis EmpleoSystem DeveloperCommented:
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
0
it_saigeDeveloperCommented:
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-
0
Francis EmpleoSystem DeveloperCommented:
Oh yeah youre right. Sorry forgot about the inheritance. Thanka for correcting :)
0
Kyle AbrahamsSenior .Net DeveloperCommented:
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.
0
AndyAinscowFreelance programmer / ConsultantCommented:
re inheritance - see the code in the very first comment.
0
AndyAinscowFreelance programmer / ConsultantCommented:
The code in the question didn't use a parent-child relationship.  Hence it didn't work.
0
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
.NET Programming

From novice to tech pro — start learning today.