Raise event c# gives error: No overload matches delegate EventHandler

时光总嘲笑我的痴心妄想 提交于 2021-01-29 19:04:13

问题


I am trying to make an event that gets raised when something happens. So other classes that contain a reference to the class that raised the event get notified. This is what i have got so far:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DropArea : MonoBehaviour {

    public event EventHandler ObjectChange;

    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }

    void OnTriggerEnter(Collider other)
    {
        var correctType = FindParentWithComponent(other.gameObject);
        if (correctType != null)
        {
            var cc = correctType.GetComponent<Object_properties>();
            if(cc.VariableTypeSelector == AcceptedVariableType)
            {
                DetectedObjectChange(null); // here i want to raise the event
            }
        }
    }

    protected virtual void DetectedObjectChange(EventArgs e)
    {
        EventHandler handler = ObjectChange;
        if (handler != null)
        {
            handler(this, e );
        }
    }  
}

this is the class that should get notified by the raised event:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IfStatement : MonoBehaviour {

    public DropArea Base, Check, Increment;

    void Start()
    {
        Base.ObjectChange += OnBaseObjectChange; //this line gives me an error so i cant compile. 
    }

    // Update is called once per frame
    void Update () {

    }

    void OnBaseObjectChange(System.Object sender)
    {
        Debug.Log("event is raised by element");
    }
}

this is the error i get :

No overload for 'OnBaseObjectChange' matches delegate 'EventHandler'

I have never worked with events before but i cant really understand how to fix this. Sadly i also dont really understand the microsoft docs about events :(

If extra clarification is needed pls let me know!
All help is really appreaciated!


回答1:


Just change this method. Because your event also requires EventArgs

void OnBaseObjectChange(System.Object sender, EventArgs e)
{
    Debug.Log("event is raised by element");
}

Delegates hold signatures of methods. As you can see your event has a method type with two parameters. Trying to use it with a single parameter causes the error.

  handler(this, e);

Alternatively, you can change the type of your event to something else. For example an Action<System.Object> to keep the current signature of your handler method, if you don't want to have an EventArgs in your event:

public event System.Action<System.Object> ObjectChange;

void OnBaseObjectChange(System.Object sender)
{
     Debug.Log("event is raised by element");
}

Where you can call it like:

handler(this);


来源:https://stackoverflow.com/questions/53924147/raise-event-c-sharp-gives-error-no-overload-matches-delegate-eventhandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!