Solved

Another SharePoint C# question

Posted on 2013-10-24
4
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

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 32

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

628 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