Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Another SharePoint C# question

Posted on 2013-10-24
4
Medium Priority
?
407 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 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 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 32

Assisted Solution

by:Jamie McAllister MVP
Jamie McAllister MVP earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

877 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