S-T
S-T

Reputation: 489

Protected attributes in c#

class A
{ 
    protected int i = 13; 
}

class B : A
{
    int i = 9;

    public void fun() {
        console.write(i);
    }
}

Upon calling method fun() from class B object , it prints the value 9.
What if i wanted to access the protected i=13 value in fun().

When protected attribute have the same name as the derived class private attribute , how to access the protected attribute of the base class within derived class ?

Upvotes: 2

Views: 1205

Answers (8)

stay_hungry
stay_hungry

Reputation: 1448

You should use base keyword for this.

Console.Write(base.i); // It prints A class attribute i

Check theses links

http://msdn.microsoft.com/en-us/library/ms173149.aspx

http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx

Upvotes: 0

dotnetmirror.com
dotnetmirror.com

Reputation: 293

 public class AcessBaseDataMember
{
   public static void Main()
   {
       DerivedClass obj = new DerivedClass();
       obj.Get_Base_DataMember();
       obj.Get_Derived_DataMember();
       Console.Read();
   }


}
public class BaseClass
{
    protected int i = 13;
}

public class DerivedClass:BaseClass { int i = 9; public void Get_Derived_DataMember() { Console.WriteLine(i); } public void Get_Base_DataMember() { Console.WriteLine(base.i); } }

Upvotes: 0

user1444049
user1444049

Reputation:

Sana Tahseen, having class members with the same name in a base and in a derived class is considered bad practice and the compiler will generate a warning message. I suggest you avoid such practices, but if you absolutely need to do this, then you should also use the new keyword in the definition of the member in the derived class:

class A
{    
    protected int i = 13;    
}

class B : A
{
    protected new int i = 9;

    public void fun()
    {
        Console.Write(base.i);
    }
}

The recommended way, however, is not to use members with the same name. You can change the value of the inherited protected member field in the constructor of the derived class:

class A
{    
    protected int i = 13;    
}

class B : A
{
    public B()
    {
        i = 9;
    }

    public void fun()
    {
        Console.Write(i);
    }
}

Upvotes: 1

Mike Coxeter
Mike Coxeter

Reputation: 639

The variable i in the B class is hiding the variable i in class A. It smells of a design issue. But, if you really want to access i from the fun method you can do it like this:

class A {    
    protected int i = 13;    
};

class B : A {    
    int i = 9;

    public void fun() {
        console.write( "B's i == " + i );
        console.write( "A's i == " + base.i );    
    }
}

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51634

class A
{ 
    protected int i = 13; 
}

class B : A
{
    int i = 9;

    public void print_own_member() {
        console.write(i);
    }

    public void print_base_class_member() {
        console.write(base.i);
    }
}

Upvotes: 0

pete the pagan-gerbil
pete the pagan-gerbil

Reputation: 3166

If you want to access it from within your derived class, try:

 class B : A
  {
    public void fun()
    {
      Console.Write(base.i);
    }
  }

Upvotes: 2

Guffa
Guffa

Reputation: 700342

Use the base keyword to access the inherited class:

Console.Write(base.i);

(Code is tested, and it uses the member in the A class.)

Upvotes: 2

the_joric
the_joric

Reputation: 12226

This would be a bad idea anyway. But you may try base.i.

Upvotes: 3

Related Questions