in C#, How to access base class variable value from child class

c# base constructor
c# base class
how to call base class constructor from derived class in c#
c# access parent properties from child class
c# call overridden method
c# subclass constructor
base vs this c#
c# extend class

Base Class

class TestBase
{
    protected int a;
    protected int b;

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

    protected TestBase()
    {

    }

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

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

Child Class

class TestChild:TestBase
{
    private string msg;
    public void Update (string s)
    {
        msg = s+ a.ToString();
        Console.WriteLine("msg=" + 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. Obviously the child class did not access base class variable int a. how can I do that?

If you'd like to get the expected result, put a static modifier front of a and/or b variables, like this:

  static protected int a;
  static protected int b;

After this, if you push that button it will write out:

msg=Hello World 100

in C#, How to access base class variable value from child class , How to get child class to access base class variable, in this case int a class but does not give the child class instance the updated value for a. >>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.

The object you created in TestBtn_Click has nothing to do with the object you created in TestBase.Update. They are two different objects so each have their own a field with different values.

To produce the output you expect, you need to set child.a to this.a in Update:

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

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

The important thing to understand here is that inheritance does not affect objects. Inheritance only affects classes. The base class members will also be present in the subclass. That's it. Even though objA's type inherits from objB, they are unrelated unless they are the same object.

In C#, how to access base class variable from child class? Solutions , Learn to use inheritance in C# libraries and applications. using System; public class A { private int value = 10; public class B : A { public int IsFamilyOrAssembly) access = " Protected Internal "; if (method. test for reference equality; that is, to be equal, two object variables must refer to the same object. A child class or derived class can access all the public, protected, internal and protected internal member. Private member cannot be accessed by child class however it is inherited and still present in child class and can be accessed using public property (GET SET modifier).

First of all, it's not clear what you're trying to accomplish.

There are many problems with this code. First, you should not be creating an instance of TestChild inside a TestBase class method. If you create an instance of TestBase inside btnTest_Click, then there is no way you'll be able to access any of TestChild's methods or data (except that you are creating an instance of TestChild inside TestBase which is bad practice).

The purpose of inheritance is usually to extend the data/methods for a base class, not to use an inherited class inside a base class. Also, with a set of inherited classes, you can achieve polymorphism which is another core principle of Object Oriented Programming. You should get a better understanding of these principles and then the code will start making more sense.

Inheritance in C#, C# Class Inheritance is one of the primary concepts of object-oriented Using the base keyword, you can access any of a base class public or  One way can be as suggested by Jon Skeet to create a Abstract base class and implement the required members in Children Classes. Other way round can be to assign the required values to members of base class in derived class constructor using base construct.

     public void Update(int i)
     {
     a = i;
     TestChild child = new TestChild(); 
     child.Update("Hello World "); 
     }

This is where you are assigning value to variable a, and later you instantiated another instance of TestChild class which is different. After this instantiation you have to assign the value like

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

Then you shall get your desired result

Lesson 8: C# Class Inheritance, C# How to access a parent class variable when parent class isn't In the example above the Child class contains 2 bool values, Foo and Bar. Using the base keyword, you can access any of a base class public or protected class members. The output from the Child print() method is on output lines 3 and 4. Another way to access base class members is through an explicit cast. This is done in the last statement of the Child class Main() method.

The child object inside your TestBase doesn't know where is he placed. It doesn't know it is inside of a class.

When you create the child you also create a totally different TestBase object which is not the same as what contains the child. When you create that object a will be initialized to a default value which is 0 in this case.

If you would like to carry the value of a to an other object you should give it to it. For example through the constructor.


Call like this:

TestChild child = new TestChild(a);

Make a constructor like this:

public TestChild(int x) : base(x){}

Unfortunately your concept bleeding from everywhere so it will not work in this form. I recommend to study inheritance more.

C# How to access a parent class variable when parent class isn't , It also teaches you to access base class private member in the child class. A child class or derived class can access all the public , protected , internal and internal int inter_var= 7;; private int pri_var = 8;; public int Private_variable; {; get set; {; pri_var = value;; }; }; }; class childclass : baseclass; {; public void check()  C# Inheritance : modify base class variable from derived class Can I set x's value in the derived class's constructor I thought I should make it some type of

C# Inheritance Member Access with Programming Example, Now I have Class B variables which I also need to use in Class A. And also one another Class C, Class A add in Class C (not inherits), and I  It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class. The class whose members are inherited is called the base class. The class that inherits the members of the base class is called the derived class. C# and .NET support single inheritance only. That is, a class can only inherit from a single class.

Can I use a child class variables in its parent class?, More restrictive access to a derived class method in Java · Parent and Child A reference variable of a superclass can be used to a refer any subclass object  base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Referencing Subclass objects with Subclass vs Superclass , In C#, you can specify that instance variables and methods are public, protected, all derived (child) classes to be able to access the method or variable, but you don't We create a Parent class containing a private and a protected variable. In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below. In the below code we declare a constructor in a derived class. We have used the ':base()' keyword after the constructor declaration with a specific parameter list.

Comments
  • Why are you creating a TestChild instance inside TestBase.Update? That doesn't make sense.
  • You should create TestChild int your main not in your Base class
  • Why the downvote? He is clearly stated his question: what he did so far, what the wrong result is and what he is expecting. If you don't like how he is coding then you have a good theme for your answer. Stop stealing points from people.