3

I am pretty new in C# and .NET and I have the following problem.

In my code I have something like this:

Table table = new Table();

lstLabelType.SelectedIndexChanged += new System.EventHandler(SelectDocumentTypeChanged);

that is attaching an event handler to a dropdown element into a view.

So when the user change the chosen value into this dropdown the SelectDocumentTypeChanged() method is performed, this one:

private void SelectDocumentTypeChanged(object sender, EventArgs e)
{
    Debug.WriteLine("SelectDocumentTypeChanged() STARTED");

    SPWeb contextWeb = SPContext.Current.Web;

    DropDownList listaTipiDocumenti = (DropDownList)sender;
    tipoDocumentoSelezionato = listaTipiDocumenti.SelectedValue;
    this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);

    string url = contextWeb.Url;
    string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
    SPUtility.Redirect(link, SPRedirectFlags.Default, Context);


}

Now I have a problem. I have to pass to this event handler method the Table table object created in my code because into this method I have to use it.

How can I correctly implement this behavior? How can I pass this Table table object to the SelectDocumentTypeChanged() method when it is automatically called when the user change the value in my dropdown?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

6

Try writing your code like this:

Table table = new Table();

lstLabelType.SelectedIndexChanged += (sender, e) =>
{
    Debug.WriteLine("SelectDocumentTypeChanged() STARTED");

    SPWeb contextWeb = SPContext.Current.Web;

    DropDownList listaTipiDocumenti = (DropDownList)sender;
    tipoDocumentoSelezionato = listaTipiDocumenti.SelectedValue;
    this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);

    string url = contextWeb.Url;
    string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
    SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
};

Now you can simply use table directly in the event handler.


You can even ignore the sender now and do this:

lstLabelType.SelectedIndexChanged += (s, e) =>
{
    Debug.WriteLine("SelectDocumentTypeChanged() STARTED");

    SPWeb contextWeb = SPContext.Current.Web;

    tipoDocumentoSelezionato = lstLabelType.SelectedValue;
    this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);

    string url = contextWeb.Url;
    string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
    SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
};

You can do this if you want to keep a separate method:

Table table = new Table();

lstLabelType.SelectedIndexChanged += (o, e) => SelectDocumentTypeChanged(lstLabelType, table);


private void SelectDocumentTypeChanged(DropDownList lstLabelType, Table table)
{
    Debug.WriteLine("SelectDocumentTypeChanged() STARTED");

    SPWeb contextWeb = SPContext.Current.Web;

    tipoDocumentoSelezionato = lstLabelType.SelectedValue;
    this.renderizzaEtichetteFacoltative(tipoDocumentoSelezionato, table);

    string url = contextWeb.Url;
    string link = url + "/ARXEIA WEBPART/Stampa Etichetta.aspx?IsDlg=1&postazione=" + macchina + "&tipoDoc=" + tipoDocumentoSelezionato;
    SPUtility.Redirect(link, SPRedirectFlags.Default, Context);
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I prefer to keep it as a separate method, is it possible? – AndreaNobili Feb 13 '19 at 09:51
  • @AndreaNobili - Why do you prefer to do that? It's poor encapsulation. – Enigmativity Feb 13 '19 at 09:52
  • To keep this logic separate and have less code into my "main" class – AndreaNobili Feb 13 '19 at 09:54
  • 1
    @AndreaNobili - It really depends on your point-of-view. My method keeps the logic together with your event code making refactoring easier and it enables proper encapsulation. – Enigmativity Feb 13 '19 at 09:56
  • Nice Solution. But be aware of a [problem](https://stackoverflow.com/a/2465054/9364460) that comes with it (even though i cant come up with a better solution) – Hyarus Feb 13 '19 at 09:57
  • 1
    @Hyarus - It's a super easy problem to solved. Just keep a reference to the handler to detach later or use `System.Reactive.Disposables.Disposable.Create` (NuGet `System.Reactive`) to create an `IDisposable` that you can use to clean up the handler - you don't need to even keep a reference to the original handler then. – Enigmativity Feb 13 '19 at 10:00