Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to declare events? Pin
Andy_L_J3-Nov-12 22:09
Andy_L_J3-Nov-12 22:09 
GeneralRe: How to declare events? Pin
Richard MacCutchan4-Nov-12 2:03
mveRichard MacCutchan4-Nov-12 2:03 
AnswerRe: How to declare events? Pin
OriginalGriff3-Nov-12 21:33
mveOriginalGriff3-Nov-12 21:33 
GeneralRe: How to declare events? Pin
Andy_L_J3-Nov-12 22:01
Andy_L_J3-Nov-12 22:01 
GeneralRe: How to declare events? Pin
OriginalGriff3-Nov-12 22:10
mveOriginalGriff3-Nov-12 22:10 
GeneralRe: How to declare events? Pin
Andy_L_J3-Nov-12 22:26
Andy_L_J3-Nov-12 22:26 
GeneralRe: How to declare events? Pin
OriginalGriff3-Nov-12 22:32
mveOriginalGriff3-Nov-12 22:32 
GeneralRe: How to declare events? Pin
DaveyM694-Nov-12 5:46
professionalDaveyM694-Nov-12 5:46 
I'm assuming this is WPF code? I say this because I have seen this exactly that sort of code.

This is different in 2 ways to 'normal' C# code:

  1. The method signature. There is nothing wrong with what you have, but normally (in non WPF) the EventArgs instance is passed as a parameter:
    C#
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    This is of no consequence as your code will function exactly the same, but I thought I'd mention it.
  2. This is more important. There is a potential race condition with your null test. It is possible that PropertyChanged could become null between the check and you calling it. I have seen this too in much WPF code, so maybe there is some internal locking or something going on behind the scenes in WPF. The normal way would be to create a copy and call the copy:
    C#
    PropertyChangedEventHandler eh = PropertyChanged;
    if(eh != null)
    {
        eh(...);
    }


So, to sumarise, I would write your code like this:
C#
public event PropertyChangedEventHandler PropertyChanged;
 
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
    PropertyChangedEventHandler eh = PropertyChanged;
    if(eh != null)
        eh(this, e);
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: How to declare events? Pin
Andy_L_J4-Nov-12 12:37
Andy_L_J4-Nov-12 12:37 
GeneralRe: How to declare events? Pin
DaveyM694-Nov-12 19:23
professionalDaveyM694-Nov-12 19:23 
QuestionC# Search Button Pin
MikeK322-Nov-12 10:57
MikeK322-Nov-12 10:57 
AnswerRe: C# Search Button Pin
fjdiewornncalwe2-Nov-12 11:19
professionalfjdiewornncalwe2-Nov-12 11:19 
GeneralRe: C# Search Button Pin
MikeK322-Nov-12 11:23
MikeK322-Nov-12 11:23 
GeneralRe: C# Search Button Pin
fjdiewornncalwe2-Nov-12 11:26
professionalfjdiewornncalwe2-Nov-12 11:26 
GeneralRe: C# Search Button Pin
MikeK322-Nov-12 11:51
MikeK322-Nov-12 11:51 
GeneralRe: C# Search Button Pin
fjdiewornncalwe2-Nov-12 11:53
professionalfjdiewornncalwe2-Nov-12 11:53 
Questioncase insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 9:31
Blake Miller2-Nov-12 9:31 
AnswerRe: case insensitive string comparison - relative speed Pin
Eddy Vluggen2-Nov-12 9:43
professionalEddy Vluggen2-Nov-12 9:43 
GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:26
Blake Miller2-Nov-12 10:26 
GeneralRe: case insensitive string comparison - relative speed Pin
Eddy Vluggen2-Nov-12 10:29
professionalEddy Vluggen2-Nov-12 10:29 
GeneralRe: case insensitive string comparison - relative speed Pin
SledgeHammer012-Nov-12 11:20
SledgeHammer012-Nov-12 11:20 
AnswerRe: case insensitive string comparison - relative speed Pin
Richard Deeming2-Nov-12 9:47
mveRichard Deeming2-Nov-12 9:47 
GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:28
Blake Miller2-Nov-12 10:28 
AnswerRe: case insensitive string comparison - relative speed Pin
Clifford Nelson2-Nov-12 10:18
Clifford Nelson2-Nov-12 10:18 
GeneralRe: case insensitive string comparison - relative speed Pin
Blake Miller2-Nov-12 10:28
Blake Miller2-Nov-12 10:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.