tags:

views:

33

answers:

1

Let's say in ASP.NET you have a base page class which defines a private Page_Load method which handles Page.Load. All aspx pages in your project inherit from this base page class. In this case the aspx pages would be able to call this private method. However it does not feel right that private methods can be called outside its own class? If so, privates methods cannot be hidden from outside? Can someone explain it?

+2  A: 

Not private methods but protected methods. That's exactly what that keyword is for.

You can read up on the different access modifiers here, and on the protected keyword in particular here.

However, the way you're planning to use it, a neater structure would probably be to make the base class method a virtual method, so that you can override it in other methods and call base.Page_Load from there.

David Hedlund
Hi mate, I have tested if you have a private Page_load event handler in the base page class, it gets called automatically in the drived classes when the pages load. How do you explain that?
Aperture
@user370401: this is probably due to auto event wireup, which uses reflection. http://it.toolbox.com/blogs/coding-dotnet/autoeventwireup-attribute-9312
David Hedlund