C# 事件可以';不能提高

C# 事件可以';不能提高,c#,events,C#,Events,我正试图做一项指定的作业,但我没有做到。我写了一个产品类,并从中得到了一朵花。然后我想提出一个事件,当花朵数量低于20时,它应该给我一个警告。我想我在提出这个问题上有困难。我确信我已经纠正了代表和事件的偏差,但是遗漏了一些东西。先谢谢你 这条线 flower.StockDecreased(); 给我这个错误: Error 3 The event 'StokTakip.Product.StockDecreased' can only appear on the left hand s

我正试图做一项指定的作业,但我没有做到。我写了一个产品类,并从中得到了一朵花。然后我想提出一个事件,当花朵数量低于20时,它应该给我一个警告。我想我在提出这个问题上有困难。我确信我已经纠正了代表和事件的偏差,但是遗漏了一些东西。先谢谢你

这条线

flower.StockDecreased();  
给我这个错误:

Error   3   The event 'StokTakip.Product.StockDecreased' can only appear on the left hand side of += or -= (except when used from within the type 'StokTakip.Product')  


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StokTakip
{
class Program
{
    static void FlowerStockDecreased()
    {
        Console.WriteLine("Flower stock decreased");
    }

    static void Main(string[] args)
    {
        Product flower = new Product("Flower", 50);
        Console.WriteLine(flower);

        flower.StockDecreased += new Product.FlowerEventHandler(FlowerStockDecreased);

        while (true)
        {
            Console.WriteLine("What is your choice");
            Console.WriteLine("[1] Stock entry quantity ");
            Console.WriteLine("[2] Stock exit quantity: ");                

            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                Console.Write("Enter stock entry quantity: ");
                flower.quantity += Convert.ToInt32(Console.ReadLine());

            }

            else if (choice == 2)
            {
                Console.Write("Enter stock exit quantity: ");
                flower.quantity -= Convert.ToInt32(Console.ReadLine());
            }              

            Console.WriteLine(flower);

            if (flower.quantity<20)
            {
                flower.StockDecreased();  //????

            }              
        }
    }       
}

}错误信息非常清楚。你不能举办那样的活动。只有声明类可以像这样调用事件,从而引发事件。所有其他类只能从事件中添加(
+=
)或删除(
-=
)事件处理程序

您可以将一个公共方法放入
Product
类中,该类引发如下事件:

public void RaiseStockDecreased()
{
    if (StockDecreased != null)
        StockDecreased();
}
public class Product
{
    public event FlowerEventHandler StockDecreased; 

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set
        {
            int newQuantity = value;
            if (newQuantity < _quantity)
            {
               if (StockDecreased != null) StockDecreased();
            }
            _quantity = newQuantity;
        }
    }

    // Other stuff
}
你可以称之为外部


但是,这又与正确的设计相矛盾,因为我希望
产品
类本身能够确定库存是增加了还是减少了,并引发正确的事件。否则,您必须在每个希望收到库存变化通知的地方实现该逻辑。

您的产品类应该如下所示:

public void RaiseStockDecreased()
{
    if (StockDecreased != null)
        StockDecreased();
}
public class Product
{
    public event FlowerEventHandler StockDecreased; 

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set
        {
            int newQuantity = value;
            if (newQuantity < _quantity)
            {
               if (StockDecreased != null) StockDecreased();
            }
            _quantity = newQuantity;
        }
    }

    // Other stuff
}
公共类产品
{
公共事件发生次数减少;
私人国际单位数量;
公共整数
{
获取{返回_数量;}
设置
{
int newQuantity=数值;
如果(新数量<\u数量)
{
如果(stockreduced!=null)stockreduced();
}
_数量=新数量;
}
}
//其他东西
}

虽然更好的模式可能是使用
StockChange
方法,只需检查变化是积极的还是消极的,并引发适当的事件。

都在错误消息中。公共成员只提供两种方法-添加和删除。除了拥有事件的类之外,你不能从任何地方引发事件。这真的很聪明。设置时检查数量。谢谢你抽出时间。我想我应该在产品类而不是其他类中做这些事情。正如Thorsten所说,如果我不这样实现,我应该在每个使用它的类中创建一个方法。