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