Solved

Another SharePoint C# question

Posted on 2013-10-24
4
390 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now