event handler
-
Hi, When should i use this:
protected virtual void OnEvent_PageMenu_Info(string msg)
{
if (Event_PageMenu_Info != null)
{ Event_PageMenu_Info(msg); }
}and this:
Event_PageMenu_Info(msg);
Sometimes i use one or the other but dont know why :laugh: Thanks :-D
nelsonpaixao@yahoo.com.br trying to help & get help
-
Hi, When should i use this:
protected virtual void OnEvent_PageMenu_Info(string msg)
{
if (Event_PageMenu_Info != null)
{ Event_PageMenu_Info(msg); }
}and this:
Event_PageMenu_Info(msg);
Sometimes i use one or the other but dont know why :laugh: Thanks :-D
nelsonpaixao@yahoo.com.br trying to help & get help
Assuming Event_PageMenu_Info is an event.... What happens if you raise the event before checking for null and there's nobody subscribed to the event? NULL reference exception :) If you've implemented a OnEvent_PageMenu_Info(), then that should be used to raise te event. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi, When should i use this:
protected virtual void OnEvent_PageMenu_Info(string msg)
{
if (Event_PageMenu_Info != null)
{ Event_PageMenu_Info(msg); }
}and this:
Event_PageMenu_Info(msg);
Sometimes i use one or the other but dont know why :laugh: Thanks :-D
nelsonpaixao@yahoo.com.br trying to help & get help
Yeah, as Mark said. Plus wrapping it in a method allows derived classes to raise the event as well.
-
Hi, When should i use this:
protected virtual void OnEvent_PageMenu_Info(string msg)
{
if (Event_PageMenu_Info != null)
{ Event_PageMenu_Info(msg); }
}and this:
Event_PageMenu_Info(msg);
Sometimes i use one or the other but dont know why :laugh: Thanks :-D
nelsonpaixao@yahoo.com.br trying to help & get help
Everything the others have said. Also, Microsoft recommend (and use themselves) a copy to handle the possibility of the event becoming null after the null check but before it's raised. This is the way I implement something like this:
using System;
public class MyPage
{
// use EventHandler<TEventArgs> for maintenance simplicity
public event EventHandler<PageMenuInfoChangedEventArgs> PageMenuInfoChanged;
// private variable
private object _PageMenuInfo;
// public property
public object PageMenuInfo
{
get { return _PageMenuInfo; }
set
{
if (_PageMenuInfo != value)
{
_PageMenuInfo = value;
// pass event args to method
OnPageMenuInfoChanged(new PageMenuInfoChangedEventArgs("Changed!"));
}
}
}
protected virtual void OnPageMenuInfoChanged(PageMenuInfoChangedEventArgs e)
{
// copy event
EventHandler<PageMenuInfoChangedEventArgs> eh = PageMenuInfoChanged;
if (eh != null)
// raise event using standard pattern
eh(this, e);
}
}
public class PageMenuInfoChangedEventArgs : EventArgs
{
public PageMenuInfoChangedEventArgs(string message)
{
_Message = message;
}
private string _Message;
public string Message
{
get { return _Message; }
}
}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) -
Assuming Event_PageMenu_Info is an event.... What happens if you raise the event before checking for null and there's nobody subscribed to the event? NULL reference exception :) If you've implemented a OnEvent_PageMenu_Info(), then that should be used to raise te event. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
He could add a do-nothing event handler inside the class that throws the event so that the invocation list is never empty.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
He could add a do-nothing event handler inside the class that throws the event so that the invocation list is never empty.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:
public event EventHandler<...EventArgs> MyEvent = delegate { };
Then the event is never
null
. Nick---------------------------------- Be excellent to each other :)
-
I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:
public event EventHandler<...EventArgs> MyEvent = delegate { };
Then the event is never
null
. Nick---------------------------------- Be excellent to each other :)
ooooohhhhhh - good tip. That deserves a 5. :)
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:
public event EventHandler<...EventArgs> MyEvent = delegate { };
Then the event is never
null
. Nick---------------------------------- Be excellent to each other :)
Excellent - nice tip Nick - 5'd :-D
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) -
Hi, When should i use this:
protected virtual void OnEvent_PageMenu_Info(string msg)
{
if (Event_PageMenu_Info != null)
{ Event_PageMenu_Info(msg); }
}and this:
Event_PageMenu_Info(msg);
Sometimes i use one or the other but dont know why :laugh: Thanks :-D
nelsonpaixao@yahoo.com.br trying to help & get help
Thanks everone for the posts :-D
nelsonpaixao@yahoo.com.br trying to help & get help