Solved

Another SharePoint C# question

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

22 Experts available now in Live!

Get 1:1 Help Now