0

I have project, where is Client class. I want to have an event for creation a Client's object. I read information about events but I still in a fog how to use events This is my code:

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);
    }
}
Vlad
  • 279
  • 1
  • 3
  • 12
  • Man I'm really not sure what you are actually trying to achieve. Maybe put some more description and fix your spelling mistakes maybe – MichaC Nov 11 '14 at 17:32
  • I'm confused -you want an event to fire whenever a new object is created or you want to fire an event that creates a new object? Events need something to trigger them, so the second case is unclear (what would trigger the event?) – D Stanley Nov 11 '14 at 17:33
  • I want to have event to create instansce of Client – Vlad Nov 11 '14 at 17:35

2 Answers2

2

I’m not sure where I should start… Events belong to an object and need to be subscribed to. So having the event on the object that you create is not really helpful since you would have to subscribe to the event before the object is actually created (that’s not possible). So instead, the event should be there, where the CreateNewClient method lives. Speaking of which, you probably want it to return the created client too. And that method should raise the event.

A proper solution would probably look something like this:

// 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);
    }
}

Used like this:

// 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");
poke
  • 369,085
  • 72
  • 557
  • 602
  • Could you explain this part of code:"handler(this, e)" . It's not clear for me how to use it espessialy "EventArgs e" ? – Vlad Nov 11 '14 at 19:20
  • Please see [this answer](http://stackoverflow.com/a/282741/216074) for details. In short, you store a thread-safe reference to the event handler, and then call that stored event handler if it’s not null. – poke Nov 11 '14 at 19:31
0

That event, with this code, will pretty much never work; because the object didn't exist, and the event is raised immediately after creation.

In other words: No one had an opportunity to register for the event!

If you really want this behavior, consider using a Factory:

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

         if (ClientCreated != null)
             ClientCreated();

         return retValue;
     }
}

Now you have a persistent event that client objects can register for. Note that static isn't the best way to implement it; its just the fastest to type. Feel free to use a better implementation of the Factory pattern.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117