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

Peter Pang
Peter Pang used Ask the Experts™
on
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 ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance programmer / Consultant
Commented:
>>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

Kyle AbrahamsSenior .Net Developer
Commented:
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.
Commented:
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-
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Francis EmpleoSystem Developer

Commented:
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

Commented:
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 EmpleoSystem Developer

Commented:
Oh yeah youre right. Sorry forgot about the inheritance. Thanka for correcting :)
Kyle AbrahamsSenior .Net Developer

Commented:
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.
AndyAinscowFreelance programmer / Consultant

Commented:
re inheritance - see the code in the very first comment.
AndyAinscowFreelance programmer / Consultant

Commented:
The code in the question didn't use a parent-child relationship.  Hence it didn't work.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial