0

In Visual Studio 2015 appears a CS0079 compile error when I tried to run this code:

public delegate void CostumeLOD(AvatarCustomization costume);

public event CostumeLOD OnCostumeLOD
        {
            add
            {
                CostumeLOD costumeLOD01 = this.OnCostumeLOD;
                CostumeLOD costumeLOD02;
                do
                {
                    costumeLOD02 = costumeLOD01;
                    costumeLOD01 = Interlocked.CompareExchange(ref this.OnCostumeLOD, (CostumeLOD)Delegate.Combine(costumeLOD02, value), costumeLOD01);
                }
                while (costumeLOD01 != costumeLOD02);
            }
            remove
            {
                CostumeLOD costumeLOD01 = this.OnCostumeLOD;
                CostumeLOD costumeLOD02;
                do
                {
                    costumeLOD02 = costumeLOD01;
                    costumeLOD01 = Interlocked.CompareExchange(ref this.OnCostumeLOD, (CostumeLOD)Delegate.Remove(costumeLOD02, value), costumeLOD01);
                }
                while (costumeLOD01 != costumeLOD02);
            }
        }

The event "OnCostumeLOD" can only appear on the left hand side of +=' or-=' operator

Someone can help me find a way to solve this error?

Clemente
  • 21
  • 1
  • 4
  • 4
    What are you actually trying to do by this code? – Vlad DX Oct 28 '15 at 22:32
  • I'm trying built some events for a Avatar morphing mesh with LOD, is for change the some items like hair, clothing. – Clemente Oct 28 '15 at 22:37
  • 1
    Your code defines an event called `OnCostumeLoad`, and then the first line in the `add` tries to assign the event to a variable of type `CostumeLOD`. This makes no sense. I don't think the `event` keyword does what you think it does. – BJ Myers Oct 28 '15 at 23:26
  • @BJMyers: [the use of the `event` keyword is fine here](https://msdn.microsoft.com/en-us/library/bb882534.aspx). Most people use the auto-generated implementation the compiler provides, but one can in fact explicitly define the implementation, as in the code above. – Peter Duniho Oct 28 '15 at 23:35
  • Yes, maybe the problem is far behind at code, need check it I will post when solve it. This morph mesh avatar is turning complicated. – Clemente Oct 29 '15 at 01:17
  • Possible duplicate of [-event- can only appear on the left hand side of += or -=](https://stackoverflow.com/questions/4496799/event-can-only-appear-on-the-left-hand-side-of-or) – StayOnTarget Feb 20 '19 at 17:06

1 Answers1

0

I am unconvinced that implementing the event explicitly is really necessary here. You seem to be trying to make it thread-safe, but the compiler-provided implementation already does a reasonable job of that.

That said, if you want the code to work, you need to declare your own backing field for the event. For example:

private CostumeLOD _onCostumeLOD;
public event CostumeLOD OnCostumeLOD
{
    add
    {
        CostumeLOD costumeLOD01 = this._onCostumeLOD;
        CostumeLOD costumeLOD02;
        do
        {
            costumeLOD02 = costumeLOD01;
            costumeLOD01 = Interlocked.CompareExchange(ref this._onCostumeLOD, (CostumeLOD)Delegate.Combine(costumeLOD02, value), costumeLOD01);
        }
        while (costumeLOD01 != costumeLOD02);
    }
    remove
    {
        CostumeLOD costumeLOD01 = this._onCostumeLOD;
        CostumeLOD costumeLOD02;
        do
        {
            costumeLOD02 = costumeLOD01;
            costumeLOD01 = Interlocked.CompareExchange(ref this._onCostumeLOD, (CostumeLOD)Delegate.Remove(costumeLOD02, value), costumeLOD01);
        }
        while (costumeLOD01 != costumeLOD02);
    }
}


Note: I am making no claims as to whether the above actually solves whatever thread-safety issue you are trying to address. I am simply offering advice as to how to get it to compile.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • public delegate void CostumeLOD(AvatarCustomization costume); – Clemente Oct 29 '15 at 00:51
  • @Clemente: I'm not talking about the declaration of the `delegate` type named `CostumeLOD`. I am talking about the **_field_** that is needed to hold the _state_ of the event. Normally, the C# compiler provides this for you; when you use the automatic implementation of an event, the name you give the event is actually treated as a _field_ by any code in the same class where the event is declared. If you explicitly implement the event, you have to provide that field, as I have done in the code example above in my answer, and which you have _not_ done in your own code. – Peter Duniho Oct 29 '15 at 01:31
  • Thank you Peter, I implemented the field and the compiler don't give any error. I have to do some tests to see if the events are working as I want. Appreciated the help! – Clemente Oct 29 '15 at 01:46