Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Can appear on the left side of an assignment operator?

The term which appears on the left-hand side of the assignment operator (usually a variable) is called the 'target'. You will always assign a value to some variable but not to a constant.

What is invalid left-hand side in assignment?

The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere. For example, a single = sign was used instead of == or === .

Can a function call statement appear on the left-hand side of an assignment operator show with suitable code snippet?

This means that whichever conditional-expression that resolves to an assignable object will do. But because in C doesn't exist the concept of reference object, you can't directly use a function on the left side of an assignment. It is instead possible in C++ where it exist.


With an explicit event, you need to provide your own backing store - either a delegate field or something like EventHandlerList. The current code is recursive. Try:

private EventHandler itemsProcessed;
public event EventHandler ItemsProcessed
{
    add
    {
        itemsProcessed-= value;
        itemsProcessed+= value;
    }

    remove
    {
        itemsProcessed-= value;
    }
}

Then (and noting I'm being a little cautious about the "about to turn null" edge-case re threading):

var snapshot = itemsProcessed;
if(snapshot != null) snapshot(this, EventArgs.Empty);

With more recent C# versions, this can be simplified:

itemsProcessed?.Invoke(this, EventArgs.Empty);

I can't tell from your post if you are trying to raise the event from a derived class or not, but one thing I've found is that you can't define an event in a base class and then raise it (directly) in a derived class, for some reason that isn't real clear to me yet.

So I define protected functions in base classes to raise events (that are defined in those base classes), like this:

// The signature for a handler of the ProgressStarted event.
// title: The title/label for a progress dialog/bar.
// total: The max progress value.
public delegate void ProgressStartedType(string title, int total);

// Raised when progress on a potentially long running process is started.
public event ProgressStartedType ProgressStarted;

// Used from derived classes to raise ProgressStarted.
protected void RaiseProgressStarted(string title, int total) {
    if (ProgressStarted != null) ProgressStarted(title, total);
}

Then in the derived class, I call RaiseProgressStarted(title, total) instead of calling ProgressStarted(title, total).

It seems like kind of the long way around. Maybe someone else knows of a better way around this problem.


It seems that if you implement the EventHandler explicitly, you can't refer to the 'Property' when firing the event. You must refer to the backing store.


What error? I guess its stack overflow error, because you are calling add and remove on yourserlf (same event). Also you cannot raise event ACCESSOR.

Valid way to do this is to create backing private event, that will be added and removed to from public accessor, and you should raise this private event.

Dang, minute late.