Thursday, March 29, 2012

please can someone explain the following code.

Hi Christian,

Thanks for the reply, but this is what I don't understand.... (by the way
there was a typo in my last message: "dpPageInit" should be "doPageInit").

I am new to inheritance, but I understand that if I create a classA that
inherits from classB then classA will expose properties/methods of classB.
The things I don't understand are code like:

a) override
b) +=
c) this.Init += new System.EventHandler

I am trying to create a page template (ie: a template page that contains a
header and footer), and then I want each of my pages to inherit the template
page so all I have to do is add the main body of the ASP page (the header
and footer will automatically be rendered by the template page).

I have downloaded the code from the following place
http://www.aspnetui.com/templates/ but I cant get it to work when I try it
myself. On top of that I think they have made the example more complex than
it needs to be.

All I want is a template that looks just like their red example. If someone
could post a simplified version of it it would be great.

"Christian" <pleasenononospamcmillotti@dotnet.itags.org.novasoftware.it> wrote in message
news:%23go0RMpVDHA.2316@dotnet.itags.org.TK2MSFTNGP09.phx.gbl...
> Hi Suzy
> It seems to me that you are in an inherited class, you are overriding
> virtual method OnInit in order to subscribe dbPageInit to be called in the
> Init Event, dbPageLoad to be called In the Load Event and calling OnInit
> method on base class ( that inherited from )...
> What else aren't you understanding?
> In case give details..
> Christian.
> "suzy" <me@dotnet.itags.org.nospam.com> ha scritto nel messaggio
> news:%23%23Wf4EpVDHA.532@dotnet.itags.org.TK2MSFTNGP09.phx.gbl...
> > please can someone explain the following code to a newbie... thanks
> > override protected void OnInit(EventArgs e)
> > {
> > this.Init += new System.EventHandler(dpPageInit);
> > this.Load += new System.EventHandler(doPageLoad);
> > base.OnInit(e);
> > }a) override:
If you have:
Class MyClass
{
...
protected virtual void MyMethod()
{
// Some instructions
...
}
}
MyMethod is the first implementation of the method.
You could have your ownn MyMethod in your class:
Class MySecondClass : Myclass
{
protected override void MyMethod()
{
// other instructions
...
}
}
You can override only virtual methods.

b) += :
add something to... for example:

c) this.Init += new System.EventHandler( doPageInit ):
add doPageInit method to delegate list called by Init event.
( Is Init an event ? )
When Init event is fired your class iterates through delegate: (
System.EventHandler is our delegate )
and call all methods subscribed
you subscribe your own method ( that must have the signature given by
delegate as return type, parameters, etc ) by writng
... += new System.EventHandler( doPageInit ):

Do you know what delegates are?

"suzy" <me@.nospam.com> ha scritto nel messaggio
news:eOF6lbpVDHA.2328@.TK2MSFTNGP12.phx.gbl...
> Hi Christian,
> Thanks for the reply, but this is what I don't understand.... (by the way
> there was a typo in my last message: "dpPageInit" should be "doPageInit").
> I am new to inheritance, but I understand that if I create a classA that
> inherits from classB then classA will expose properties/methods of classB.
> The things I don't understand are code like:
> a) override
> b) +=
> c) this.Init += new System.EventHandler
>
> I am trying to create a page template (ie: a template page that contains a
> header and footer), and then I want each of my pages to inherit the
template
> page so all I have to do is add the main body of the ASP page (the header
> and footer will automatically be rendered by the template page).
> I have downloaded the code from the following place
> http://www.aspnetui.com/templates/ but I cant get it to work when I try it
> myself. On top of that I think they have made the example more complex
than
> it needs to be.
> All I want is a template that looks just like their red example. If
someone
> could post a simplified version of it it would be great.

> > "suzy" <me@.nospam.com> ha scritto nel messaggio
> > news:%23%23Wf4EpVDHA.532@.TK2MSFTNGP09.phx.gbl...
> > > please can someone explain the following code to a newbie... thanks
> > > > override protected void OnInit(EventArgs e)
> > > {
> > > > this.Init += new System.EventHandler(dpPageInit);
> > > this.Load += new System.EventHandler(doPageLoad);
> > > base.OnInit(e);
> > > }
> >
> Thanks for your reply. No I don't know what delegates are. :-(

should take a look..
> Also, in your example of MyClass/MyMethod in which order will the code be
> run?
MyClass.MyMethod is 1 implementation
MySecondClass.MyMethod is another 1
they are not tied
public Class MyClass
{
...
protected virtual void MyMethod()
{
// Some instructions
MessageBox.Show( "1" );
}
protected virtual void AnotherMethod()
{
MessageBox.Show( "Hello" );
}
}
MyMethod is the first implementation of the method.
You could have your ownn MyMethod in your class:
public Class MySecondClass : Myclass
{
protected override void MyMethod()
{
// other instructions
MessageBox.Show( "2" );
...
}
protectd override AnotherMethod()
{
base.AnotherMethod();
MessageBox.Show( "Suzy" );
}
}
MyClass cl = new MyClass();
cl.MyMethod(); // displays 1
cl.AnotherMethod(); // displays Hello
MySecondClass cl2 = new MySecondClass();
cl2.MyMethod(); // displays 2
cl2.AnotherMethod(); // displays Hello and then Suzy

talking about delegates and events

this.Load += new System.EventHandler(doPageLoad);
this.Load += new System.EventHandler(doPageAnotherLoad);
this.Load += new System.EventHandler(doPageAnotherOneLoad);

On Load Fired will be executed
doPageLoad(), doPageAnotherLoad(), doPageAnotherOneLoad(), i think in order
of submission

> Someone said the following code would create a template if placed in a
page
> template class:
Sorry, i'm not the proper person for help you with asp... never worked :-(
Repost for detailed and specific helps.

> protected override void OnInit (EventArgs args)
> {
> this.Controls.AddAt(0, LoadControl("path to header.ascx " );
> base.OnInit(e);
> this.Controls.Add(LoadControl("path to footer.ascx"));
> }
> And the code in my page that inherited the above contained the
following...:
> override protected void OnInit(EventArgs e)
> {
> InitializeComponent();
> base.OnInit(e);
> }
> private void Page_Load(object sender, System.EventArgs e)
> {
> placeholder.Controls.Add (grdDataGrid);
> }
> They said this should place my placeholder/datagrid between a
header/footer.
Thanks, that explains a lot! Much appreciated!

I think I am right in saying that if I inherited a template class containing
the code below in it, then it won't place a header/footer around my ASP code
(of the page that inherits the template class).

protected override void OnInit (EventArgs args)
{
this.Controls.AddAt(0, LoadControl("path to header.ascx" );
base.OnInit(e);
this.Controls.Add(LoadControl("path to footer.ascx"));
}

"Tom" <TomRemoveThisClement@.sbcglobal.net> wrote in message
news:uMKACrzVDHA.2156@.TK2MSFTNGP11.phx.gbl...
> Hi Susy,
> A delegate (or an event) is an kind of pointer to functions. The
difference
> between an event and an ordinary pointer is that inside of the event is a
> list of pointers to functions. If you "call" the event it's called
raising
> the event and every function that has been added to the event is called in
> order.
> The way this is typicaly used in C# is if you have a class in which things
> can occur that might be of interest to others, you can declare a public
> event (like "event EventHandler Load;") Any other object that is
interested
> in (i.e wants to be notified of) this occurance (Load) registers this
> interest by adding a pointer to a function in the event. So when you see
> the code:
> this.Load += new System.EventHandler(doPageLoad);
> you are looking at an expression of interest in the Load event. When it
> occurs, the doPageLoad() function will be called (along with any other
> functions that have been registered with the Load event using the +=
syntax.
> The odd thing about this code is that it it registering with an event on
the
> same object as the current instance. There are better (or at least more
> natural) ways of accomplishing this. First, if (as is likely) the Load
> event is declared in a superclass (a class from which this current class
was
> derived - directly or indirectly) you would typically override a protected
> OnLoad() method to obtain the notification of Load. Second, if the event
is
> declared in the same object as the += code, you'd typically just call a
> function directly instead of raising an event and receiving notification
for
> it.
> Tom
> "suzy" <me@.nospam.com> wrote in message
> news:%23PoQfkrVDHA.3088@.tk2msftngp13.phx.gbl...
> > great, i am starting to understand more:
> > i thought it worked the way you say it worked, that's why i didn't
> > understand the theory behind he code that was submitted to me regarding
> the
> > page hearder/footer (shown below):
> > > protected override void OnInit (EventArgs args)
> > > {
> > > this.Controls.AddAt(0, LoadControl("path to header.ascx " );
> > > base.OnInit(e);
> > > this.Controls.Add(LoadControl("path to footer.ascx"));
> > > }
> > if the above code is in my base/template class (A), i can't see how i
can
> > write code in a class that inherits from it (B), so that my code from B
> gets
> > inserted between the header/footer.
> > i know you said you haven't done asp.net, but doesn't the theory of it
> sound
> > wrong to you?
> > "Christian" <pleasenononospamcmillotti@.novasoftware.it> wrote in message
> > news:uE5jGJrVDHA.532@.TK2MSFTNGP10.phx.gbl...
> > > > > Thanks for your reply. No I don't know what delegates are. :-(
> > > > should take a look..
> > > > > > Also, in your example of MyClass/MyMethod in which order will the
code
> > be
> > > > run?
> > > MyClass.MyMethod is 1 implementation
> > > MySecondClass.MyMethod is another 1
> > > they are not tied
> > > public Class MyClass
> > > {
> > > ...
> > > protected virtual void MyMethod()
> > > {
> > > // Some instructions
> > > MessageBox.Show( "1" );
> > > }
> > > protected virtual void AnotherMethod()
> > > {
> > > MessageBox.Show( "Hello" );
> > > }
> > > }
> > > MyMethod is the first implementation of the method.
> > > You could have your ownn MyMethod in your class:
> > > public Class MySecondClass : Myclass
> > > {
> > > protected override void MyMethod()
> > > {
> > > // other instructions
> > > MessageBox.Show( "2" );
> > > ...
> > > }
> > > protectd override AnotherMethod()
> > > {
> > > base.AnotherMethod();
> > > MessageBox.Show( "Suzy" );
> > > }
> > > }
> > > MyClass cl = new MyClass();
> > > cl.MyMethod(); // displays 1
> > > cl.AnotherMethod(); // displays Hello
> > > MySecondClass cl2 = new MySecondClass();
> > > cl2.MyMethod(); // displays 2
> > > cl2.AnotherMethod(); // displays Hello and then Suzy
> > > > talking about delegates and events
> > > > this.Load += new System.EventHandler(doPageLoad);
> > > this.Load += new System.EventHandler(doPageAnotherLoad);
> > > this.Load += new System.EventHandler(doPageAnotherOneLoad);
> > > > On Load Fired will be executed
> > > doPageLoad(), doPageAnotherLoad(), doPageAnotherOneLoad(), i think in
> > order
> > > of submission
> > > > > > > Someone said the following code would create a template if placed in
a
> > > page
> > > > template class:
> > > Sorry, i'm not the proper person for help you with asp... never worked
> :-(
> > > Repost for detailed and specific helps.
> > > > > protected override void OnInit (EventArgs args)
> > > > {
> > > > this.Controls.AddAt(0, LoadControl("path to header.ascx " );
> > > > base.OnInit(e);
> > > > this.Controls.Add(LoadControl("path to footer.ascx"));
> > > > }
> > > > > > And the code in my page that inherited the above contained the
> > > following...:
> > > > > > override protected void OnInit(EventArgs e)
> > > > > > {
> > > > > > InitializeComponent();
> > > > > > base.OnInit(e);
> > > > > > }
> > > > > > private void Page_Load(object sender, System.EventArgs e)
> > > > > > {
> > > > > > placeholder.Controls.Add (grdDataGrid);
> > > > > > }
> > > > > > They said this should place my placeholder/datagrid between a
> > > header/footer.
> > > >

0 comments:

Post a Comment