473,289 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

What is "Protected" doing?

This is an issue regarding what exactly is accomplished by using "Protected"
when defining a variable. It seems it does much more than just applying
Protected status to a variable.

I have an ascx control named HeadingBar. I have dragged it onto an aspx
page.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar

This defines HeadingBar1 but, of course, does not instantiate it so
HeadingBar1 is nothing. (no suprise here)
If I use the following statement.....

Protected HeadingBar1 as HeadingBar

This not only defines HeadingBar1 but it also instantiates it as
ASP.HeadingBar_ascx. Here it seems that "Protected" is doing more than
giving a variable protected status - it's instantiating an instance!
Everything works fine when this is used.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar = new HeadingBar

It instantiates HeadingBar1 as myProjectName.HeadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Title = "This is
the heading" is executed the Title property in the ascx control throws an
exception on the Set statement of that property saying that lblTitle.text is
not instantiated.

It seems "Protected" is performing some functionality that I cannot find in
the docs. Can anyone explain this?

Thanks,
T


May 18 '06 #1
4 1797
I think you're confusing accessibility with instantiation. The variable is
accessible, or visible, to the asp page but is not instantiated. If you test
it, the variable is null because it isn't instantiated. when you're defining
the HeadingBar1, without the protected accessibility descriptor, it's
defined as private which means it's only visible within the class and not
within the asp page containing it.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
This is an issue regarding what exactly is accomplished by using
"Protected" when defining a variable. It seems it does much more than
just applying Protected status to a variable.

I have an ascx control named HeadingBar. I have dragged it onto an aspx
page.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar

This defines HeadingBar1 but, of course, does not instantiate it so
HeadingBar1 is nothing. (no suprise here)
If I use the following statement.....

Protected HeadingBar1 as HeadingBar

This not only defines HeadingBar1 but it also instantiates it as
ASP.HeadingBar_ascx. Here it seems that "Protected" is doing more than
giving a variable protected status - it's instantiating an instance!
Everything works fine when this is used.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar = new HeadingBar

It instantiates HeadingBar1 as myProjectName.HeadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Title = "This is
the heading" is executed the Title property in the ascx control throws an
exception on the Set statement of that property saying that lblTitle.text
is not instantiated.

It seems "Protected" is performing some functionality that I cannot find
in the docs. Can anyone explain this?

Thanks,
T

May 18 '06 #2
I'm asuming you are using asp.net 1.1, if thats the case here is what I
understand. First of all, protected is an access modifier meaning that
only the class itself or any other class that inherits from it can
access that field, method, property, etc. In your case, when it
declares the control as Protected, it gives the ability for your .aspx
pages to declare controls and use it within the page. If you are
curious, if you look at the <%@ Control %> directive or <%@ Page %>
directive in your .aspx page you will see that it inherits from the
CodeBehind page. If you remove the protected keyword from the control
declaration, then when you run the page it will complain that it can't
find the control that you delared in your aspx page. So, when doing
CodeBehind, the Protected keywork is necessary. Does this give you some
idea of whats going on?

May 18 '06 #3
Tina:

The instance is created when you drop the ascx on the page. Everything else
is just a matter of whether you are referencing it or not...
Protected allows a child class access to the member. By doing Protected
HeadingBar as HeadingBar, it's allowing the child class (the aspx file) to
access it. Essentially, the aspx page is doing base.HeadingBar1 = new
HeadingBar() when you drop the control on there.

Protected doesn't do anything magical, it merely allows a child class to
access it. ASP.NET automatically associates member variables with control
ids. but in order to work, the aspx page (child) which inherits from the
codebehind (parent) has to have access to the property...so it must be
protected or public.
Dim HeadingBar1 as HeadingBar = new HEadingBar
creates a new instance of the class. What you need to do is to access the
existing instance is:

dim HeadingBar1 as HeadingBar = ctype(Page.FindControl("HeadingBar1"),
HeadingBar)

to create a new instance you need to do:

dim headingBar as HEadingBar = Page.LoadControl("~/headingbar.ascx");
someContainer.COntrols.Add(headingBar)

(for arguments sake, we can say that Page.LoadControl = new)
Either line is declaring a variable. In either case, dropping the control on
the aspx page is what creates the instance. Declaring it protected merely
lets ASP.NET assign the instance to the variable, as must be since the aspx
inherits from the codebehind..
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
This is an issue regarding what exactly is accomplished by using
"Protected" when defining a variable. It seems it does much more than
just applying Protected status to a variable.

I have an ascx control named HeadingBar. I have dragged it onto an aspx
page.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar

This defines HeadingBar1 but, of course, does not instantiate it so
HeadingBar1 is nothing. (no suprise here)
If I use the following statement.....

Protected HeadingBar1 as HeadingBar

This not only defines HeadingBar1 but it also instantiates it as
ASP.HeadingBar_ascx. Here it seems that "Protected" is doing more than
giving a variable protected status - it's instantiating an instance!
Everything works fine when this is used.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar = new HeadingBar

It instantiates HeadingBar1 as myProjectName.HeadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Title = "This is
the heading" is executed the Title property in the ascx control throws an
exception on the Set statement of that property saying that lblTitle.text
is not instantiated.

It seems "Protected" is performing some functionality that I cannot find
in the docs. Can anyone explain this?

Thanks,
T

May 18 '06 #4
Got It. Thanks ( and thanks to Mark and tdavisjr too)
T

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:eY**************@TK2MSFTNGP03.phx.gbl...
Tina:

The instance is created when you drop the ascx on the page. Everything
else is just a matter of whether you are referencing it or not...
Protected allows a child class access to the member. By doing Protected
HeadingBar as HeadingBar, it's allowing the child class (the aspx file) to
access it. Essentially, the aspx page is doing base.HeadingBar1 = new
HeadingBar() when you drop the control on there.

Protected doesn't do anything magical, it merely allows a child class to
access it. ASP.NET automatically associates member variables with control
ids. but in order to work, the aspx page (child) which inherits from the
codebehind (parent) has to have access to the property...so it must be
protected or public.
Dim HeadingBar1 as HeadingBar = new HEadingBar
creates a new instance of the class. What you need to do is to access the
existing instance is:

dim HeadingBar1 as HeadingBar = ctype(Page.FindControl("HeadingBar1"),
HeadingBar)

to create a new instance you need to do:

dim headingBar as HEadingBar = Page.LoadControl("~/headingbar.ascx");
someContainer.COntrols.Add(headingBar)

(for arguments sake, we can say that Page.LoadControl = new)
Either line is declaring a variable. In either case, dropping the control
on the aspx page is what creates the instance. Declaring it protected
merely lets ASP.NET assign the instance to the variable, as must be since
the aspx inherits from the codebehind..
Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
This is an issue regarding what exactly is accomplished by using
"Protected" when defining a variable. It seems it does much more than
just applying Protected status to a variable.

I have an ascx control named HeadingBar. I have dragged it onto an aspx
page.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar

This defines HeadingBar1 but, of course, does not instantiate it so
HeadingBar1 is nothing. (no suprise here)
If I use the following statement.....

Protected HeadingBar1 as HeadingBar

This not only defines HeadingBar1 but it also instantiates it as
ASP.HeadingBar_ascx. Here it seems that "Protected" is doing more than
giving a variable protected status - it's instantiating an instance!
Everything works fine when this is used.

If I use the following statement.....

Dim HeadingBar1 as HeadingBar = new HeadingBar

It instantiates HeadingBar1 as myProjectName.HeadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Title = "This
is the heading" is executed the Title property in the ascx control throws
an exception on the Set statement of that property saying that
lblTitle.text is not instantiated.

It seems "Protected" is performing some functionality that I cannot find
in the docs. Can anyone explain this?

Thanks,
T


May 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jules Dubois | last post by:
I'm want to create a superclass with nothing but attributes and properties. Some of the subclasses will do nothing but provide values for the attributes. (I'd also like to make sure (1) that the...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
4
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
3
by: Jordan Taylor | last post by:
I am confused about protected member functions and objects. Is there any particular advantage of declaring members protected?
2
by: Rob Richardson | last post by:
Greetings! Consider the following: class CBase { public: CBase(); virtual ~CBase();
3
by: eBob.com | last post by:
I'm getting this error on this statement ... Dim TA_rect As Rectangle = CType(Me.ClientRectangle.memberwiseclone(), Rectangle) "Me" inherits from UserControl. I'm not an OOD guru but maybe...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.