scope of private vs protected objects

  • Thread starter Thread starter Jason Shohet
  • Start date Start date
J

Jason Shohet

A label on an ascx control has a corresponding declaration in the c#
code-behind page.
I was curious what would happen if I made that declaration 'PRIVATE' instead
of 'PROTECTED'.
The only things that touch that label are functions in that code-behind page
itself -- nothing on the outside.

Basically, it doesn't work. Only when I changed it back to 'protected' in
the ascx's cs page, could I modify the text in the label, from within
functions in that cs page. I was curious why this is.

I always thought 'Private' means that "anything on that class can access the
private / protected object".

TY for the explanation
 
A label on an ascx control has a corresponding declaration in
the c# code-behind page.
I was curious what would happen if I made that declaration
'PRIVATE' instead of 'PROTECTED'.
The only things that touch that label are functions in that
code-behind page itself -- nothing on the outside.

Basically, it doesn't work. Only when I changed it back to
'protected' in the ascx's cs page, could I modify the text in
the label, from within functions in that cs page. I was curious
why this is.

I always thought 'Private' means that "anything on that class
can access the private / protected object".

Jason,

There are two classes in an .aspx page: the abstract parent class
(in the code-behind file), and the concrete child class (in the .aspx
file).

The members of the abstract class in the code-behind file need to
have a protected scope so they will be visible when .Net creates an
instance of the .aspx page concrete child class.

See MSDN for more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/vbcon/html/vbconwebformscodemodel.asp

or

http://tinyurl.com/k82i


Hope this helps.

Chris.
 
The page class that's actually instantiated is a subclass of the code-behind
class, so private members aren't accessable.
 
currently as of ASP.NET 1.1, the .aspx file is parsed upon request, and a new class is generated inheriting from your code behind class. so visibility of inheritance rules apply here. anything in your codebehind base class that you wish the child (.aspx) class to see and display needs to be protected. now with new partial class feature in C# 2.0, this should change. you shall be able to declare them as private in 2005

----- Jason Shohet wrote: ----

A label on an ascx control has a corresponding declaration in the c
code-behind page
I was curious what would happen if I made that declaration 'PRIVATE' instea
of 'PROTECTED'
The only things that touch that label are functions in that code-behind pag
itself -- nothing on the outside

Basically, it doesn't work. Only when I changed it back to 'protected' i
the ascx's cs page, could I modify the text in the label, from withi
functions in that cs page. I was curious why this is

I always thought 'Private' means that "anything on that class can access th
private / protected object"

TY for the explanatio
 
Thanks guys.
This new partial class feature sounds very cool. We have to wait until
2005... I wonder if there's a beta of the next .NET version out there
somewhere.
 

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

Similar Threads


Back
Top