C#处理事件 [英] C# Handle Event
问题描述
我试图做一个排序库中,我试图抓住我怎么能实现它,他们的方式,我想。
我创建了一个极简的例子来告诉你什么,我试图做的。
I am trying to make sort of a library and i am trying to grasp how can i implement it they way i want. I created a minimalist example to show you what i am trying to do.
using System;
namespace example
{
public class Car
{
public int Price;
public string ModelName;
public Boolean Sold;
public delegate void SellEventHandler(string str);
public static event SellEventHandler _OnSell;
public void OnSell(string str)
{
Console.WriteLine("event was fired");
}
public Car(int price, string modelname)
{
Price = price;
ModelName = modelname;
Sold = false;
_OnSell = OnSell;
}
}
public class Program
{
static void Main()
{
Car _car = new Car(6000, "audi");
_car._OnSell += Car_OnSell;
}
public void Car_OnSell(string message)
{
Console.WriteLine(message);
}
}
}
虽然我还没有实施在事件将被调用(它应该被调用时,销售
的属性 _car
的变化),我想执行车
类的 OnSell(字符串str)
办法(打印事件被解雇),并在此之后,我想执行 Car_OnSell
功能(见代码 _car.OnSell + = Car_OnSell
)
Even though i haven't implemented when the event will be invoked ( it should be invoked when the Sold
property of the _car
changes ), i want to execute the OnSell(string str)
method of the Car
class ( prints "event was fired" ) and after that, i want to execute the Car_OnSell
function ( see code _car.OnSell += Car_OnSell
)
希望你得到什么,我想在这里做的想法。现在,我得到的错误是会员'example.Car._OnSell不能用一个实例引用来访问;与类型名称,而不是
就行了限定它 _car.OnSell + = Car_OnSell;
。但是我不知道如果我在正确的方向与此去的。
Hopefully you get the idea of what i am trying to do here. Right now the error i get is Member 'example.Car._OnSell' cannot be accessed with an instance reference; qualify it with a type name instead
on the line _car.OnSell += Car_OnSell;
. However i am not sure if i am going in the right direction with this at all.
推荐答案
我想我明白你的重新做的,这里是我会怎么做。
I think I understand what you're doing, and here's how I would do it.
- 请不要在你的类挂钩的事件。相反,创建一个卖出的方法,做什么类通常会做(如集
销售==真
),但首先检查客户端是否迷上了你的_OnSell
事件,火灾,第一。您可能要提供一些方式为客户取消了_OnSell
事件中的销售以及 - 您还需要你的
Car_OnSell
静态的,因为你是从一个静态方法挂钩起来(主
)。这是因为非静态方法需要一个类的实例来访问它
- Don't hook up an event in your class. Instead, create a 'Sell' method that does whatever the class would normally do (like set
Sold == true
), but first check if the client hooked up your_OnSell
event, and fire that first. You may want to provide some way for the client to cancel the sale in the_OnSell
event as well. - You also need to make your
Car_OnSell
static, since you're hooking it up from a static method (Main
). This is because a non-static method requires a class instance to access it.
下面是一个例子:
static void Main()
{
var car = new Car(6000, "audi");
car._OnSell += Car_OnSell;
car.Sell(string.Format("Selling the car: {0}", car.ModelName));
}
public static void Car_OnSell(string message)
{
Console.WriteLine(message);
}
public class Car
{
public int Price { get; set; }
public string ModelName { get; set; }
public Boolean Sold { get; set; }
public delegate void SellEventHandler(string str);
public event SellEventHandler _OnSell;
public void Sell(string str)
{
if (_OnSell != null)
{
_OnSell(str);
}
this.Sold = true;
}
public Car(int price, string modelname)
{
Price = price;
ModelName = modelname;
Sold = false;
}
}
这篇关于C#处理事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!