Solved

Another SharePoint C# question

Posted on 2013-10-24
4
397 Views
Last Modified: 2013-10-24
I am taking a course in SP dev. The instructor assumes that one understands everything about C#, so he does certain things without explanation. I don't have a lot of C# experience, but I have somewhat of a "decent" understanding of it, but I think it is very important for me to know why he is doing certain things that he does not explain.

First he does this

public class BasicWebPart : WebPart
    {
       Button btn = null;
        protected override void CreateChildControls()
        {
            btn = new Button();
            btn.Text = "Click Me";
            btn.Click += new EventHandler(btn_Click);
            Controls.Add(btn);
            var literal = new LiteralControl(@"<p>Some Text"</p>);
            Controls.Add(literal);
        }

        void btn_Click(object sender, EventArgs e)
        {
            btn.Text = "Clicked";
        }
    }

I understand why btn was declared outside of the scope of CreateChildControls() so that it could be used in the event handler code as well. But why did a new instance of btn have to be created inside of CreateChildControls(), but it doesn't have to be created inside of the event handler code? I think it is because btn is coming from the object sender portion of the event handler, so that is why an instance of it has to be created in CreateChildControls, but this instance persists for the event handler code because it is being persisted and passed by the call to the event handler code inside of CreateChildControls. But if that is the case, then why did it need to be declared at the top outside of createChildControls and the event Handler code if it is merely persisting from instance created within CreateChildControls?


 And why doesn't the event handler routine need anything like public or private? Is that because it is a delegate rather than a class?
0
Comment
Question by:BobHavertyComh
  • 2
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 39596782
If it isn't declared as Public, Private et al then accessibility is Private for class members;

http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Button is instantiated only once, because you only have one button. There's no need to create another instance in Button Click as it already exists (otherwise it couldn't have been clicked!)

Why didn't it get instantiated up front before the CreateChildControls? Because we don't instatiate objects too early, just when we need them. If something happened such as an exception and CreateChildControls was never reached we wouldn't want the garbage collector to have to hunt down an unnecessary instance.
0
 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39596884
Button is instantiated only once, because you only have one button. There's no need to create another instance in Button Click as it already exists (otherwise it couldn't have been clicked!)

But why then did it even need to be declared at the top? i thought that was because it's persistence ends after CreateChildControls ended. And if the call to the onclick that occurs in CreateChildControl passes the persistence of this button instance through object sender, then why is the top declaration even needed?

Sorry for the nitpick questions, but i really need to nail a good understanding of this down.
0
 
LVL 31

Assisted Solution

by:Jamie McAllister MVP
Jamie McAllister MVP earned 500 total points
ID: 39596889
Declaring it at the top gave it the scope to exist right across the class. If it'd been declared within a method it couldn't exist outside the method.

This is not the same as instantiating it in a method. Scope comes from where its declared not where its instantiated.
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 39597204
Thanks Jaime
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question