Ben
Ben

Reputation: 1321

Custom Event Conversion vb.net to c#

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.

Upvotes: 2

Views: 682

Answers (1)

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31738

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 -=

Upvotes: 2

Related Questions