controls sometimes private & sometimes protected

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When you add a control to a form in a windows application , It will be
private by default
but
in a web application it will be protected by default , as well as , it can
be changed to private .

WHY ???
 
Hadidi,

This is because in in the case of Windows Forms the VS generates the class
of the form or control as it is going to be used by the application. They
are accessible from the form's or controls own code.
In ASP.NET form thought, the class that you design in VS is used as a base
class for a new class generated by ASP.NET compiler when the page is
compiled to be served to the web browser. This new class has code to build
the page structure and thus the control cariable needs to be assesbile by
this derived class in order to be initialized. This is the reason why the
variable needs to be protected.
 
Hadidi said:
When you add a control to a form in a windows application , It will be
private by default
but
in a web application it will be protected by default , as well as , it can
be changed to private .

WHY ???

In a web application, from what I remember (and it may have changed in
ASP.NET 2.0 where partial classes could solve the problem more
elegantly) the code-behind forms the base class, and the aspx code
creates a class deriving from that class. The aspx code may well need
to access the controls, which is why they're protected.

At least, that's my guess :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top