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
ChildClass
Calling
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 ?
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 ");
}
}
{
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);
}
}
{
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);
}
{
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 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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).
-saige-
protected: Access is limited to the containing class or types derived from the containing class.Another proof of concept.
- Source
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}");
}
}
}
Which produces the following output -It 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-
Oh yeah youre right. Sorry forgot about the inheritance. Thanka for correcting :)
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.
re inheritance - see the code in the very first comment.
The code in the question didn't use a parent-child relationship. Hence it didn't work.