473,385 Members | 1,856 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,385 software developers and data experts.

private vs protected declaration of User Controls

Nad
Hello,

When I add a User Control to a web form and then try to declare it in the
code as a private member it doesn't get instantiated. However with
'protected' it's OK!

For example I get a runtime error on the line indicated in
InitializeComponent() because the user control is null. However if I declare
it as 'protected' it's ok.

private myUserControl userControl;
....
InitializeComponent()
{
....
userControl.Click += new EventHandler(userControl_Click);
}
Nov 19 '05 #1
3 1655
That is because the .aspx is turned into a class that actually inherits from
the class in your .vb file. The .aspx contains the declaration of your
control. The name you give it there, needs to match up to a variable in the
class. Now, if this variable is declared as private, the .aspx class cannot
see it - and thus there is a disconnect there, and the object does not get
instantiated. If it is protected, the then .aspx class can see it (because
it inherits from the .vb class), and it is matched up to the object declared
in the .aspx itself.

"Nad" <Na*@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Hello,

When I add a User Control to a web form and then try to declare it in the
code as a private member it doesn't get instantiated. However with
'protected' it's OK!

For example I get a runtime error on the line indicated in
InitializeComponent() because the user control is null. However if I
declare
it as 'protected' it's ok.

private myUserControl userControl;
...
InitializeComponent()
{
...
userControl.Click += new EventHandler(userControl_Click);
}

Nov 19 '05 #2
Hi,

it is due to the code model of asp.net so that it involves two classes, one
being the one you code (the code-behind class) and the other being one
generated dynamically when the markup is parsed (ascx, aspx etc). This class
created dynamically from markup inherits from your code-behind class and
therefore for members to be visible through the classes (and so that they
can be set to point to the controls), members must be at least protected.

In ASP.NET v2 this has changed due to partial classes so that no member
declarations for controls are needed.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #3
Nad
Thank you all.
Nov 19 '05 #4

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

Similar topics

3
by: Corno | last post by:
Hi all, I was just wondering why a class declaration also includes the private members of a class. Is this information needed by other classes for compiling and/or linking or is this just an...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
4
by: Jason Shohet | last post by:
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...
4
by: Brian Lowe | last post by:
I have several aspx pages that include a couple of asp:placeholder controls, an example is myPage.aspx. The page inherits from a custom class 'myClass' which inherits System.Web.UI.Page which...
2
by: K. Shier | last post by:
Friend variables "are accessible from within the program that contains their declaration and from anywhere else in the same assembly." according to the VS.Net help. This sounds like a pretty broad...
2
by: Dennis | last post by:
I have a control that inheirts from "Panel". I want to prevent the user from having access to the MoueUp event. I tried making a sub in my control that handles MyBase.MouseUp but still this event...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
2
by: Morten71 | last post by:
Just curious. In VB, if I, from design-mode in vs2005, double-click a button (or any other control), the code-behind file opens and the default declaration is automatically inserted with method...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.