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);
var literal = new LiteralControl(@"<p>Some Text"</p>);
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?