tags:

views:

282

answers:

4

I have a class that has private fields... (cars)

I then inherit from this class... (Audi)

In the (Audi) class, when I type this. in the constructor...

the private fields are not available...

Do I need to do anything special to expose this private fields in (cars) class so that they are accessible via this. in (Audi class)?

+11  A: 

You should declare them as "protected" instead of private

Philippe Leybaert
Thanks , that worked....
JL
IMO, making fields `protected` is a **bad** idea.
Marc Gravell
Same for me, I prefer private fields and protected properties. But it's not always worth on really small projects/cases
Clement Herreman
@Clement - even with C# 3.0 automatically implemented props? So easy to do...
Marc Gravell
Whether it's a good idea or not, it's the answer to the question. I guess providing a correct answer deserves several downvotes.
Philippe Leybaert
Right, I didn't know about that. At my company where use c# 2.0. So yes, forget about protected fields. Fields are ALWAYS private. Don't let anyone touch your member, except your friends ( hehehe )
Clement Herreman
Phillipe, the purpose of votes is to sort the answers, the criterion is 'unhelpful'. So yes, -1 from me.
Henk Holterman
I agree 100% with the "protected fields are a bad idea" thought, but this question is a pure technical question about C#. "how to access a base class's fields?". I'm afraid the elaborate explanations provided in the other answers will confuse the OP even more.
Philippe Leybaert
@Philippe Leybaert : yep, -1 for me too. You could edit your answer, to make it better, and I could take my downvote off.IMO, on SO, the aim is not to provide "working" solution, but best solutions, and explain why. I sometimes come here with a question, and leave knowing that my question is a false question and that I made a bigger mistake. That's the strenght of SO, compared to Google (I think).
Clement Herreman
Guys - I got the answer, and its working for my implementation... Thanks though for all the effort..
JL
+20  A: 

One (bad) option is to make the fields protected - but don't do this; it still breaks proper encapsulation. Two good options:

  • make the setter protected
  • provide a constructor that accepts the values

examples:

public string Name { get; protected set; }

(C# 2.0)

private string name;
public string Name {
    get { return name; }
    protected set { name = value; }
}

or:

class BaseType {
  private string name;
  public BaseType(string name) {
    this.name = name;
  }
}
class DerivedType : BaseType {
  public DerivedType() : base("Foo") {}
}
Marc Gravell
+17  A: 

Philippe's suggestion to declare the fields as protected instead of private will indeed work - but I suggest you don't do it anyway.

Why should a derived class care about an implementation detail of how the data is stored? I suggest you expose protected properties which are (currently) backed by those fields, instead of exposing the fields themselves.

I treat the API you expose to derived classes as very similar to the API you expose to other types - it should be a higher level of abstraction than implementation details which you may want to change later.

Jon Skeet
+5  A: 

You are probably looking for a concept called constructor inheritance. You can forward arguments to the base classes constructor - see this example, where the Audi has a flag indicating whether it's an S-Line edition or not:

namespace ConstructorInheritance
{
    abstract class Car
    {
     private int horsePower;
     private int maximumSpeed;

     public Car(int horsePower, int maximumSpeed)
     {
      this.horsePower = horsePower;
      this.maximumSpeed = maximumSpeed;
     }
    }

    class Audi : Car
    {
     private bool isSLineEdition = false;

     // note, how the base constructor is called _and_ the S-Line variable is set in Audi's constructor!
     public Audi(bool isSLineEdition, int horsePower, int maximumSpeed)
      : base(horsePower, maximumSpeed)
     {
      this.isSLineEdition = isSLineEdition;
     }
    }

    class Program
{
 static void Main(string[] args)
 {
  Car car = new Audi(true, 210, 255);
  // break here and watch the car instance in the debugger...
 }
}    }
Marc Wittke
+1 for complementary informations that I think OP will find useful
Clement Herreman
Hmm... it's not really clear that the OP needs to do anything within the constructor. No downvote as there's no inaccurate information and it *might* help the OP, but it doesn't really address the full question.
Jon Skeet
How could he ask questions about things he does not know?IMHO it is valid to see the underlying problem rather than just answering questions on how to encapsulate variables or the difference between protected and private access modifiers.
Marc Wittke