Passing a private variable to the base class constructor

I want to pass the value to the base class constructor. The problem I am facing is that the value is stored in a private variable inside the derived class. Can this be conveyed? or is it a good approach to do this?

Here is what I tried

class Filtering : Display { private int length = 10000; public Filtering():base(length) { } } 

He shows

An object reference is required for a non-static field, method, or Property

Base class

 abstract class Display { public Display(int length) { } } 
+5
source share
1 answer

It was as a defendant that Chips_100 wrote in his answer (currently deleted by the owner):


If you want the length to be an instance variable, but still supply it to the base constructor, I would suggest something like this:

 private const int DefaultLength = 10000; private int length = DefaultLength; public Filtering() : base(DefaultLength) { } 

I did not see any indication that the author of the original of this answer is inclined to restore his own post. At the same time, although I would write basically the same thing, I would prefer not to take credit for an existing answer created by someone else. So I converted this to a response to the community wiki.

+2
source

Source: https://habr.com/ru/post/1235171/


All Articles