473,729 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.H eadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Tit le = "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 1825
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**********@n ospammeexcite.c om> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.H eadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Tit le = "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.HeadingBar 1 = 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.Find Control("Headin gBar1"),
HeadingBar)

to create a new instance you need to do:

dim headingBar as HEadingBar = Page.LoadContro l("~/headingbar.ascx ");
someContainer.C Ontrols.Add(hea dingBar)

(for arguments sake, we can say that Page.LoadContro l = 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**********@n ospammeexcite.c om> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.H eadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Tit le = "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******** ******@TK2MSFTN GP03.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.HeadingBar 1 = 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.Find Control("Headin gBar1"),
HeadingBar)

to create a new instance you need to do:

dim headingBar as HEadingBar = Page.LoadContro l("~/headingbar.ascx ");
someContainer.C Ontrols.Add(hea dingBar)

(for arguments sake, we can say that Page.LoadContro l = 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**********@n ospammeexcite.c om> wrote in message
news:%2******** ********@TK2MSF TNGP05.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.H eadingBar! Here it's an
object of my project. But when the statement HeadingBar1.Tit le = "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
6422
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 subclass provides actual values for the attributes and (2) that no "client" module adds or removes attributes or properties, but I don't know how to do those.) I don't understand what I'm doing wrong, or maybe what I want to do is impossible. ...
28
3419
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
4
7350
by: p988 | last post by:
using System; using System.Windows.Forms; using System.Drawing; class MyForm : Form { MyForm () { Text = "Windows Forms Demo"; }
3
6721
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
1652
by: Rob Richardson | last post by:
Greetings! Consider the following: class CBase { public: CBase(); virtual ~CBase();
3
4295
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 I sort of understand the problem. If so, this is not my fault, right? Since I can access ClientRectangle I don't see why I can't clone it. Is there anything easier/better than typing out all of
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.