Note that there are some explanatory texts on larger screens.

plurals
  1. POGlobal function repository
    primarykey
    data
    text
    <p>I have several <code>Control</code> such as <code>ToolStripMenuItem</code> and <code>Button</code> that need to have a function bound to their click event. They will either generate a new <code>Form</code> or add a <code>TabPage</code> to a current <code>TabControl</code>. I may end up having several different <code>Control</code> that will need the same function, so I thought of making a Global function repository.</p> <p>I would have a class named <code>Services</code> looking like this:</p> <pre><code>public class Services { TabControl container; delegate void fonction(int id); Dictionary&lt;string, fonction&gt; functions = new Dictionary&lt;string, fonction&gt;(); public Services(TabControl control) { container = control; InitFunctions(); } public Delegate Getfunction(string name) { if (functions.ContainsKey(name)) return functions[name]; else throw new NotImplementedException("Failed to instantiate " + name ); } // List of all the desired functions // Function example private void ProductLoan(int id) { string name = "Loan"+id.ToString(); Form newForm = new Loan(); newForm.Text = Properties.Resources.MakeLoan; newForm.ShowDialog(); } private void InitFunctions() { fonction f = new fonction(ProductLoan); functions.Add("Loan", f); // For each functions // f = new fonction(name); // functions.Add(name, f); } } </code></pre> <p>This class would be instantiated when the program start and stored globally so it can be accessed everywhere. <strong>Correct me if I am wrong to proceed like this, but I didn't make the <code>Services</code> class static since it needs to have an instance of the <code>TabControl</code> and to initialise the function list.</strong></p> <p>I have no clue if this is a good idea so I would appreciate some advice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload