2

I'm having some issues with converting a custom event from vb.net to c#, I have no experience of vb.net really, and i have even less experience with these custom events, the rest of the application was converted without much issue, however this one has me stuck. The converters that i have used (both paid and free) have all failed to produce usable code.

VB.net code:

<NonSerialized()> Private _objNonSerializablePropertyChangedHandlers As New System.ComponentModel.EventHandlerList

'''' <summary> 
'''' Raised when a public property of this object is set. 
'''' </summary> 
Public Custom Event PropertyChanged As PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    AddHandler(ByVal value As PropertyChangedEventHandler)
        Me.NonSerializablePropertyChangedHandlers.AddHandler(STR_PROPERTYCHANGEDEVENT, value)
    End AddHandler
    RemoveHandler(ByVal value As PropertyChangedEventHandler)
        Me.NonSerializablePropertyChangedHandlers.RemoveHandler(STR_PROPERTYCHANGEDEVENT, value)
    End RemoveHandler
    RaiseEvent(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)

        Dim obj As PropertyChangedEventHandler = TryCast(Me.NonSerializablePropertyChangedHandlers(STR_PROPERTYCHANGEDEVENT), PropertyChangedEventHandler)

        If obj IsNot Nothing Then
            obj.Invoke(sender, e)
        End If

    End RaiseEvent

the best i have managed to produce in c# is the following:

C#

[NonSerialized()]
private System.ComponentModel.EventHandlerList _objNonSerializablePropertyChangedHandlers = new System.ComponentModel.EventHandlerList();

public event PropertyChangedEventHandler PropertyChanged
{
    add
    {
        this.NonSerializablePropertyChangedHandlers.AddHandler(STR_PROPERTYCHANGEDEVENT, value);
    }
    remove
    {
        this.NonSerializablePropertyChangedHandlers.RemoveHandler(STR_PROPERTYCHANGEDEVENT, value);
    }
}

protected void OnPropertyChanged(string strPropertyName)
{
    EventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
        handler.Invoke(this, new PropertyChangedEventArgs(strPropertyName));
        //PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
    }
}

However this throws an error "The event 'CardBase.PropertyChanged' can only appear on the left hand side of += or -="

on the line : EventHandler handler = this.PropertyChanged;

As i'm not 100% on what the above code is doing, im struggling to fix this myself, I would be very grateful is someone could assist on this.

9
  • That should compile... Commented Jun 10, 2013 at 10:47
  • Not totally related to your question, but you can prevent a race condition by using the Interlocked.CompareExchange pattern to obtain your event handler. Commented Jun 10, 2013 at 10:53
  • @newStackExchangeInstance: Why do you say that? It certainly appears to be a valid compiler error... Commented Jun 10, 2013 at 10:53
  • @Chris The line he mentioned looks valid... maybe he should've put PropertyChangedEventHandler instead of EventHandler, but still. Commented Jun 10, 2013 at 10:55
  • @newStackExchangeInstance: Did you read the error it gives? I can't quote you a compiler reference for where that is but if it empirically doesn't work then saying it should without any more information isn't very helpful. Commented Jun 10, 2013 at 11:02

1 Answer 1

2

change this code

protected void OnPropertyChanged(string strPropertyName)
{
    EventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
        handler.Invoke(this, new PropertyChangedEventArgs(strPropertyName));
    }
}

to

protected void OnPropertyChanged(string strPropertyName)
{
    var handler =
       this.NonSerializablePropertyChangedHandlers[STR_PROPERTYCHANGEDEVENT]
           as EventHandler;

    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(strPropertyName));
    }
}

reference -event- can only appear on the left hand side of += or -=

Sign up to request clarification or add additional context in comments.

5 Comments

That wasn't where his error came from, it came from the top line.
EventHandler handler = this.PropertyChanged; will still throw the exact same compiler warning surely...
You're right. Overlooked it. Anyway I updated the solution. I am guessing the automatic events have some compiler magic happening. Since you are creating an explicit event, you have to access the backing store itself. Btw. the VB.NET code already does this.
Hey, thanks for the reply that did the job, i must admit i didnt have a full understanding of what this code did when looking through the vb.net code. Thanks again!
FYI: If you don't specify add / remove the compiler generates some code behind the scenes to make it thread safe stackoverflow.com/questions/15646155/… which includes a backing variable. Your code introduces a EventHandlerList as a backing variable for multiple EventHandlers for whatever reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.