使用事件创建对象

时间:2014-11-11 17:27:51

标签: c# events

我有项目,Client类在哪里。  我想要创建一个客户端对象的事件。我阅读有关事件的信息,但我仍然在雾中如何使用事件 这是我的代码:

public class Client
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string DateOfBirth { get; set; }
    public string Address { get; set; }

    public Client(string name, string surname, string dateOfBirth, string address)
    {
        FirstName = name;
        SecondName = surname;
        DateOfBirth = name;
        Address = address;
    }

    public event EventHandler NewClient;

    public virtual void OnNewClient(object sender, EventArgs e)
    {
        NewClient(this, e);
    }

class Program
{
    static void Main(string[] args)
    {
        Client client = new Client();
        client.OnNewClient();

    }


    public void CreateNewClient( string name, string surname, string dateOfBirth, string address)
    {
        string Name = name;
        string Surname = surname;
        string Date = dateOfBirth;
        string Address = address;

        Client client = new Client(Name, Surname,Date, Address);
    }
}

2 个答案:

答案 0 :(得分:2)

我不知道应该从哪里开始......事件属于一个对象,需要订阅。因此,在您创建的对象上创建事件并不是很有用,因为您必须在实际创建对象之前订阅事件(这是不可能的)。相反,事件应该在那里,CreateNewClient方法存在。说到这一点,您可能希望它也返回创建的客户端。那种方法应该引发事件。

正确的解决方案可能看起来像这样:

// Client class only cares about itself; is completely independent
public class Client
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string DateOfBirth { get; set; }
    public string Address { get; set; }

    public Client(string name, string surname, string dateOfBirth, string address)
    {
        FirstName = name;
        SecondName = surname;
        DateOfBirth = name;
        Address = address;
    }
}

// Client factory is responsible for creating clients
class ClientFactory
{
    // Event name should describe what happened
    public event EventHandler ClientCreated;

    public Client CreateNewClient(string name, string surname, string dateOfBirth, string address)
    {
        // no need to copy the arguments into new local variables again; arguments
        // are already local
        Client client = new Client(name, surname, dateOfBirth, address);

        // raise the ClientCreated event
        OnClientCreated();

        // return the created client
        return client;
    }

    // provide this proctected virtual method for raising the event
    protected virtual void OnClientCreated(EventArgs e)
    {
        var handler = ClientCreated;
        if (handler != null)
            handler(this, e);
    }

    // provide private specialized versions for convenience (e.g. to call it without
    // passing an argument)
    private void OnClientCreated()
    {
        OnClientCreated(EventArgs.Empty);
    }
}

像这样使用:

// create the client factory
ClientFactory factory = new ClientFactory();

// subscribe to the event
factory.ClientCreated += (s, e) => {
   Console.WriteLine("Client created.");
};

// create clients
Client c1 = factory.CreateNewClient("Foo", "Bar", "Baz", "Baf");
Client c2 = factory.CreateNewClient("Foo", "Bar", "Baz", "Baf");

答案 1 :(得分:0)

使用此代码,该事件几乎永远不会工作;因为该对象不存在,并且在创建后立即引发该事件。

换句话说:没有人有机会注册该活动

如果确实想要这种行为,请考虑使用工厂

public static class ClientFactory
{
     public static event Action ClientCreated;
     public static Client CreateClient(..)
     {
         Client retValue = new Client(...);

         if (ClientCreated != null)
             ClientCreated();

         return retValue;
     }
}

现在您有一个客户端对象可以注册的持久性事件。请注意,static不是最佳方式来实现它;它只是打字最快的。随意使用更好的Factory模式实现。