【问题标题】:Declaring events - "event must be of delegate type"声明事件 - “事件必须是委托类型”
【发布时间】:2016-08-01 11:25:27
【问题描述】:

我在制作活动时遇到了一些麻烦。我收到错误'IStregsystem.UserBalanceWarning': event must be of delegate type, 但我无法弄清楚到底是什么问题。

我的界面IStregsystemevent UserBalanceNotification UserBalanceWarning; 行,我必须实现它。

我的事件类看起来像这样(我现在只是测试):

using System;

namespace Eksamensopgave2016
{
    public class UserBalanceNotification
    {
        public delegate void UserBalanceMessage(User user, decimal balanceDifference);
        public event UserBalanceMessage UserBalanceWarning;
        public User user { get; set; }
        private decimal _balanceDifference { get; }
        public void OnUserBalanceWarning()
        {
            if (UserBalanceWarning != null)
            {
                UserBalanceWarning(user, _balanceDifference);
            }
            else
            {
                Console.WriteLine("Event fired!");
            }
        }
        public UserBalanceNotification(User user, decimal balanceDifference)
        {
            _balanceDifference = balanceDifference;
            SetValue(user, balanceDifference);
        }

        public void SetValue(User user, decimal balanceDifference)
        {
            if (balanceDifference != 0)
            {
                user.Balance += balanceDifference;
                if (user.Balance < 50)
                {
                    OnUserBalanceWarning();
                }
            }        
        }
    }
}

我只是对事件有点不确定。 线条

if (UserBalanceWarning != null)
{
    UserBalanceWarning(user, _balanceDifference);
}

UserBalanceWarning 是一个方法,还是一个属性?

感谢您的帮助,谢谢!

我的IStregsystem

using System;
using System.Collections.Generic;
using Eksamensopgave2016.Core;

namespace Eksamensopgave2016
{
    public interface IStregsystem
    {
        IEnumerable<Product> ActiveProducts { get; }
        List<User> Users { get; set; }
        Queue<Transaction> Transactions { get; set; }
        InsertCashTransaction AddCreditsToAccount(User user, int amount);
        BuyTransaction BuyProduct(User user, Product product);
        Product GetProductByID(int productID);
        IEnumerable<Transaction> GetTransactions(User user, int count);
        List<User> GetUsers(IFilter<User> filter);
        User GetUserByUsername(string username);
        event UserBalanceNotification UserBalanceWarning;
    }
}

【问题讨论】:

  • UserBalanceWarning 是一个方法,还是一个属性? 这是一个事件。
  • 你能展示你的 IStregsystem 和 UserBalanceNotification 的代码吗?
  • @Nikolay 我已经在 OP 中有我的整个 UserBalanceNotification.cs 文件。我已经编辑了帖子以包含 IStregsystem。

标签: c# events methods properties


【解决方案1】:

在 IStregsystem 接口中,您使用类类型 (UserBalanceNotification) 作为事件处理程序。这将给出上述编译时错误。

事件的处理程序应该是委托类型。您可以查看您的 UserBalanceNotification 类以了解正确的方法。

public class ClassWithEvent
{
    public delegate void CustomEventHandler(User user, decimal balanceDifference);
    public event CustomEventHandler CustomEvent;

    private void FireCustomEvent(User user, decimal balanceDifference)
    {
        if (CustomEvent != null)
        {
            CustomEvent(user, balanceDifference);
        }
    }
}

使用以下代码注册事件:

public class ClassUsingEvent
{
    private ClassWithEvent _classWithEvent;

    public ClassUsingEvent()
    {
        _classWithEvent = new ClassWithEvent();
        _classWithEvent.CustomEvent += HandleCustomEvent;
    }

    void HandleCustomEvent(User user, decimal balanceDifference)
    {
        // handle event
    }
}

【讨论】:

  • 谢谢!我将不得不进一步研究它,但我非常感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
相关资源
最近更新 更多