c# - 在 .NET 中传递事件

标签 c# events

我很抱歉发布了这么一大段代码,但在这种情况下,我觉得这个问题会更容易理解,尽管这个问题可能非常简单(我希望有一个同样简单的答案)。

我正在玩事件和委托(delegate)。在我的主要方法中,我有代码

traffic.PutInGarage(g);

这意味着我正在传递我的 Garage 类的引用(参见下面的代码)。这是您期望事件通过的方式吗?我不知道为什么,我无法解释为什么,感觉不对,好像我错过了某个地方。

再次抱歉,发布了所有控制台应用程序代码,但这可能更容易。

using System;
using System.Collections.Generic;

namespace DemoProejct
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Garage g = new Garage();
            g.NewCarEvent += new Garage.NewCarDelegate(GarageCount);

            Traffic traffic = new Traffic();

            //SHOULD I BE PASSING THE Garage object here?

            traffic.PutInGarage(g);

            Console.WriteLine("Garage is now closed");
            Console.ReadKey();
        }

        private static void GarageCount(string cars, string s)
        {
            Console.WriteLine(string.Format("{0} {1}", cars, s));
            System.Threading.Thread.Sleep(2000);
        }
    }

    public class Traffic
    {
        public void PutInGarage(Garage g)
        {
            List<Vehicle> all = GetVehicles();

            Vehicle modelWeFix = new Vehicle() { Make = "Mazda", Model = "6", Year = "2012" };
            int i = 1;
            foreach (IEquatabled<Vehicle> item in all)
            {
                if (item.EqualsTo(modelWeFix))
                {
                    g.CarsInGarage = i;
                    i++;
                }
            }
        }

   private List<Vehicle> GetVehicles()
   {
        Car carMazda = new Car() { Make = "Mazda", Model = "6", Year = "2012" };
        Car carFord = new Car() { Make = "Ford", Model = "Sport", Year = "2002" };
        Car carUnknown = new Car() { Make = "Mazda", Model = "5", Year = "2012" };
        Bike mazdaBike = new Bike() { Make = "Mazda", Model = "6", Year = "2012" };

        IEquatabled<Vehicle> unknownBike = mazdaBike;

        List<Vehicle> all = new List<Vehicle>();
        all.Add(carMazda);
        all.Add(carFord);
        all.Add(carUnknown);
        all.Add(mazdaBike);
        return all;
   }
}

    public class Garage
    {
        public delegate void NewCarDelegate(string numberOfCars, string message);
        public event NewCarDelegate NewCarEvent;

        private int _carsInGarage;
        public int CarsInGarage
        {
            get { return _carsInGarage; }
            set
            {
                if (NewCarEvent != null)
                {
                    _carsInGarage = value;
                    NewCarEvent(value.ToString(), " cars in the garage.");
                }
            }
        }

        public Garage()
        {
            CarsInGarage = 0;
        }
    }

    public class Vehicle : IEquatabled<Vehicle>
    {
        public string Make { get; set; }
        public string Model { get; set; }
        public string Year { get; set; }
        public virtual int Wheels { get; set; }

        //Implementation of IEquatable<T> interface 
        public bool EqualsTo(Vehicle car)
        {
            if (this.Make == car.Make && this.Model == car.Model && this.Year == car.Year)
                return true;
            else
                return false;
        }

    }

    public class Car : Vehicle
    {}

    public class Bike : Vehicle
    {}

    interface IEquatabled<T>
    {
        bool EqualsTo(T obj);
    }
}

最佳答案

不要将它传递给每个调用 put in garage 的调用,但创建一个带有车库的 Traffic 实例是否更有意义。

        Garage g = new Garage();
        g.NewCarEvent += new Garage.NewCarDelegate(GarageCount);

        Traffic traffic = new Traffic(g); // pass your garage here.

        traffic.PutInGarage();

关于c# - 在 .NET 中传递事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139181/

相关文章:

c# - 如果 MSDTC 被禁用,您如何绕过 TransactionScope 内的多个数据库连接?

events - GWT 事件预览与事件处理程序

javascript - AngularJS:TypeError:undefined 不是带有链式选择的函数

javascript - ReactJS onChange 事件处理程序

c++ - 处理从较低级别对象引发的事件

javascript - 在 for 循环中设置一个间隔以显示结果

c# - 检查客户端计算机上是否安装了 adobe reader

c# - C# 和 ASP.NET 中的换行符有什么变化? (\r\n 与\n)

c# - 提交 Ajax.Beginform 后更新局部 View

c# - 如何访问下面的SqlDataSource?