dimitris93
dimitris93

Reputation: 4273

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

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 )

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.

Upvotes: 2

Views: 585

Answers (1)

Rufus L
Rufus L

Reputation: 37020

I think I understand what you're doing, and here's how I would do it.

  1. 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.
  2. 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.

Here's an example:

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

Upvotes: 1

Related Questions