Getting null event in delegate
-
Hi all, I want to raise an event with delegates my code is like this: delcaration:
public delegate void ButtonClickedDelegate(string path);
public event ButtonClickedDelegate ButtonClicked;call:
GetData(path);
raise:
private void GetData(string path)
{
try
{
if (ButtonClicked!= null)
ButtonClicked(path);
}
catch
{
}
}But I am getting ButtonClicked = null at the line
if (ButtonClicked!= null)
so that
ButtonClicked(path);
is not getting executed.... Do anybody have idea what is the wrong with the code?
-
Hi all, I want to raise an event with delegates my code is like this: delcaration:
public delegate void ButtonClickedDelegate(string path);
public event ButtonClickedDelegate ButtonClicked;call:
GetData(path);
raise:
private void GetData(string path)
{
try
{
if (ButtonClicked!= null)
ButtonClicked(path);
}
catch
{
}
}But I am getting ButtonClicked = null at the line
if (ButtonClicked!= null)
so that
ButtonClicked(path);
is not getting executed.... Do anybody have idea what is the wrong with the code?
That's because you've just declared a event handler, not created it. try something like:
ButtonClicked += new System.EventHandler(GetData);
regards :)
-
Hi all, I want to raise an event with delegates my code is like this: delcaration:
public delegate void ButtonClickedDelegate(string path);
public event ButtonClickedDelegate ButtonClicked;call:
GetData(path);
raise:
private void GetData(string path)
{
try
{
if (ButtonClicked!= null)
ButtonClicked(path);
}
catch
{
}
}But I am getting ButtonClicked = null at the line
if (ButtonClicked!= null)
so that
ButtonClicked(path);
is not getting executed.... Do anybody have idea what is the wrong with the code?
Hi, you need to wire a handler to your event, as in
ButtonClicked += new ButtonClickedDelegate(handler);
Compare with what Visual Designer adds to myForm.designer.cs when you wire a handler interactively. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi all, I want to raise an event with delegates my code is like this: delcaration:
public delegate void ButtonClickedDelegate(string path);
public event ButtonClickedDelegate ButtonClicked;call:
GetData(path);
raise:
private void GetData(string path)
{
try
{
if (ButtonClicked!= null)
ButtonClicked(path);
}
catch
{
}
}But I am getting ButtonClicked = null at the line
if (ButtonClicked!= null)
so that
ButtonClicked(path);
is not getting executed.... Do anybody have idea what is the wrong with the code?
Where is the handler to the event? Guess you are missing that.
-
Hi, you need to wire a handler to your event, as in
ButtonClicked += new ButtonClickedDelegate(handler);
Compare with what Visual Designer adds to myForm.designer.cs when you wire a handler interactively. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Getting 'System.StackOverflowException' at line
void OnButtonClicked()
{}
when I do
ButtonClicked += OnButtonClicked;
...
void OnButtonClicked(...) {
...
}Let me guess: in the OnButtonClicked code, you are raising the ButtonClicked event? If yes, doing so casue the OnButtonClicked code to be recursively called non-stop, quickly filling the avaliable stack memory. In your OnButtonClicked, you have to put logic to handle the event, not to raise it.
-
Hi all, I want to raise an event with delegates my code is like this: delcaration:
public delegate void ButtonClickedDelegate(string path);
public event ButtonClickedDelegate ButtonClicked;call:
GetData(path);
raise:
private void GetData(string path)
{
try
{
if (ButtonClicked!= null)
ButtonClicked(path);
}
catch
{
}
}But I am getting ButtonClicked = null at the line
if (ButtonClicked!= null)
so that
ButtonClicked(path);
is not getting executed.... Do anybody have idea what is the wrong with the code?
ButtonClicked will be null if there are no subscribers to the event (therefore the delegate's InvocationList is empty). This example shows you how to do it. The ButtonClicked event here is initiated by the public PerformButtonClicked method being called on a class instance and in turn calling the OnButtonClicked method, but obviously in the real world the OnButtonClicked method would be called by the instance itself on response to a MouseDown or something.
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
TestClass test;
public Form1()
{
InitializeComponent();
test = new TestClass();
test.ButtonClicked += new TestClass.ButtonClickedDelegate(test_ButtonClicked);
test.PerformButtonClicked(@"C:\Windows");
}void test\_ButtonClicked(string path) { MessageBox.Show(path); } } public class TestClass { public delegate void ButtonClickedDelegate(string path); public event ButtonClickedDelegate ButtonClicked; protected virtual void OnButtonClicked(string path) { ButtonClickedDelegate handler = ButtonClicked; if (handler != null) handler(path); } public void PerformButtonClicked(string path) { OnButtonClicked(path); } }
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)