-1

Some say here that if members are protected you can access them: Are private members inherited in C#?

Did someone really tried ?

I have tried it doesn't even work with protected:

public partial class Base
{
    protected IObject myObject;
}

If I derive (Base is in another namespace but it shouldn't matter I of course import that namespace)

public partial class Derive: Base
{

}

Intellisense doesn't show myObject in Derive Class.

So what I can do if I need a myObjhect member in all my derived classes to call some methods upon ? If I have to duplicate that member then what's the use of inheritance ?

Update: I forgot Derive:Base but that was just mystypo, of course I did that.

Community
  • 1
  • 1
user310291
  • 36,946
  • 82
  • 271
  • 487
  • When you say: "Intellisense doesn't show myObject in Derive Class" you mean you're trying to access to `myObject` within a method of `Derive` class, not from the outside right ? Otherwise it's correct, you can't see it... – digEmAll Jan 30 '11 at 18:08
  • yes within a method of Derive. – user310291 Jan 30 '11 at 18:09

4 Answers4

2

You aren't deriving. To derive, you have to do the following:

public class Base
{
    protected IObject myObject;
}

public class Derive : Base
{

}

myObject is available in Derive now. Partial means you are splitting the class definition over multiple files.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

You have to actually derive from the Base class:

public partial class Derive : Base
{
}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class in order for the inherited base class method to work properly.

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

check this

Community
  • 1
  • 1
ayush
  • 14,350
  • 11
  • 53
  • 100
0

Aren't you missing the base class when deriving? Should work with myObject declared as protected.

public partial class Derive : Base
{

}
grysik44
  • 233
  • 2
  • 7