Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Private vs Protected for how a Button Click Event is Created in ASP.NET using VB.NET?

Something I have never thought too hard about but I am curiuos and want to understand the actual reasoning. In ASP.NET using VB.NET, you can define the wired up button click event (to an ASP.NET server control) in 2 different ways (for the purpose of this conversation - manually wiring up via button property not in question here):

  1. Double click on the button in the designer which produces an event in the code behind with a Protected method.
  2. In the code behind, select the button from the list of controls, and then select it's 'Click' event. This produces a Private method.

I understand the difference between Private and Protected; that is not in question here. I just want to know the actual reason (not guessing or speculating please) of why based on how the wired up event is autocreated it generates a different Access Level on the method?

Thanks!

like image 237
atconway Avatar asked Feb 22 '12 15:02

atconway


1 Answers

The reason it's protected when declared in the control itself is the the .aspx is compiled to a class on the fly that inherits from the codebehind, so it wouldn't see the method if it was private. When the event is assigned internally to the codebehind, obviously the private member is accessible.

Both are valid approaches, just depends on how you want to skin the cat, and if a control is dynamically created, you may have to assign the event in the codebehind, so that mechanism needs to be there.

like image 195
swannee Avatar answered Oct 20 '22 00:10

swannee