1

I am not able to access i in my derived class. m.i is not working. Why?

 public class MyClass
    {
        protected int i;
        public MyClass()
        {

        }

        public virtual void Display()
        {
            Console.WriteLine("Base dis");
        }
    }

    public class MyCla : MyClass
    {
        public MyCla()
        {


        }
        int ij = 12;
        public new void Display()
        {
            MyClass m = new MyClass();
            // m.i is inaccessible here
            Console.WriteLine("Derived dis");
        }
    }
0

5 Answers 5

4

That is because protected variables and properties are only available to derived classes, within the derived context (thus by referencing it with this), and to instances of the current class (so if m was MyCla it would have worked).

MyCla m = new MyCla();
int x = m.i;

An option is to make the field internal, or protected internal if you wish, which makes it available to all classes within the assembly.

Here is a compiling example.

Sign up to request clarification or add additional context in comments.

13 Comments

@Default OP intantiates MyClass in the method inside the derived class, not MyCla.
@PatrickHofman: Sorry, this looks illogical to my thoughts (Not your learn things, but this idea of program logic). Till date in 5 years of programming, I was under the assumption that I can access it this way and that is what Protected means. But strange, the definition of protected access and its behavior looks 180 degree opposite.
You can access it using this, but you are creating a new instance of a base class, that's not enough to gain access to the protected variables.
@Divine base and this relate to the current instance. When you do MyClass = new MyClass() you are creating a new instance. They are not the same thing.
I understand is can be confusing, and I have no real reason to say it is a very bad idea. The c# language team thought it was. That ends all.
|
0

The "protected" keyword means that only a type and types that derive from that type can access the member.

You have a couple of options if you want to be able to access that member

  • Make it public.
  • Make it internal. This will allow any types to access the member within the same assembly (or other assemblies should you add friend's

Cheers!

Comments

0

You can not get protected variables in child class, But that will be accessible in Child Class method or Constructor directly eg. base.i;

public class MyCla : MyClass
{
    public MyCla()
    {
        base.i=100;
    }
    int ij = 12;
    public new void Display()
    {
        MyClass m = new MyClass();
        base.i=100; // Accessible
        m.i = 100; // Not Accessible
        Console.WriteLine("Derived dis");
    }
}

if you want that variable accessible with object, than make it public.

public int i;
public MyClass()
{

}

public class MyCla : MyClass
{
    public MyCla()
    {


    }
    int ij = 12;
    public new void Display()
    {
        MyClass m = new MyClass();
        m.i = 100; // accessible
        Console.WriteLine("Derived dis");
    }
}

Thanks

1 Comment

Thank you, but that idea of programming sucked. I do not agree anymore, its illogical. Why the HECK it should be accessible with "Base" and not with "Object". The MSDN has a loads of confusion in framing their sentences or definitions for Inheritance and Protected access. I till date in 5 years of programming, thought protected means, i can access in derived class. Heck, its a shame for both me and Microsoft when it behaves differently when I am learning this today in VS console.
0

You are creating a new instance:

MyClass m = new MyClass(); 

This instance only allows you to interact with public members. Thus m.i is not working.

Inside your derived class MyCla class however you have access to the protected field.

e.g

public new void Display()
{
           i = 1; //this will work
}

3 Comments

Man, I am afraid what you saying. Have you compiled it? I am claiming it should work, but its not working, but you say it has to work.
Inside your Display method you can access the field from the derived class. But you are creating a new instance and then trying to access to the field. Remove the line MyClass m = new MyClass() and you will see that you can access the private field.
You may want to run and see. Thanks. Question is not on private but protected.
-1

You can access i from within the MyCla class but in your method "Display" you're creating a new MyClass object, hence it has nothing to do with MyCla at all anymore.

Here is a code example of how to access the variable "i":

public class MyClass
{
    protected int i;
    public MyClass()
    {

    }

    public virtual void Display()
    {
        Console.WriteLine("Base dis");
    }
}

public class MyCla : MyClass
{
    public MyCla()
    {
    }

    int ij = 12;

    public new void Display()
    {
        Console.WriteLine(this.i);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.