Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementation of delegates in C#
    primarykey
    data
    text
    <p>I am trying to learn on how to use delegates efficiently in C# and I was just wondering if anyone can guide me through... The following is a sample implementation using delegates... All I am doing is just passing a value through a delegate from one class to another... Please tell me if this is the right way to implement... And also your suggestions... </p> <p>Also, please note that I have de-registered the delegate in :</p> <pre><code>void FrmSample_FormClosing(object sender, FormClosingEventArgs e) { sampleObj.AssignValue -= new Sample.AssignValueDelegate(AssignValue); } </code></pre> <p>Is this de-registration necessary?</p> <p>The following is the code that I have written.. </p> <pre><code>public partial class FrmSample : Form { Sample sampleObj; public FrmSample() { InitializeComponent(); this.Load += new EventHandler(FrmSample_Load); this.FormClosing += new FormClosingEventHandler(FrmSample_FormClosing); sampleObj = new Sample(); sampleObj.AssignValue = new Sample.AssignValueDelegate(AssignValue); } void FrmSample_FormClosing(object sender, FormClosingEventArgs e) { sampleObj.AssignValue -= new Sample.AssignValueDelegate(AssignValue); } void FrmSample_Load(object sender, EventArgs e) { sampleObj.LoadValue(); } void AssignValue(string value) { MessageBox.Show(value); } } class Sample { public delegate void AssignValueDelegate(string value); public AssignValueDelegate AssignValue; internal void LoadValue() { if (AssignValue != null) { AssignValue("This is a test message"); } } } </code></pre> <p>Pls provide your feedback on whether this is right...</p> <p>Thanks, Ram</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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