Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a private base method from a derived class in C#

How can a derived class call a method from a base class? Other classes should not have access on the other hand.

My situation:
I have a base class, in which I wrote a private method to register some values.

private void register(string param1, int param2){//...}

I did this to allow subclasses to register different stuff. The problem is, that a derived class cannot access private methods or fields of a base class. That makes sense to me, since private means PRIVATE.

I don't want to make the method public because other classes should not be able to call this method.

Can someone provide a solution or guide me towards a better design?

like image 351
Noel Widmer Avatar asked Nov 07 '14 12:11

Noel Widmer


1 Answers

When you declare something private, only the class that defines it can access it. Not even derived classes

What you need is protected

When something is declared as protected it can be accessed by any derived class while staying hidden from other non-related classes

like image 63
dimlucas Avatar answered Sep 30 '22 03:09

dimlucas