Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# event is null

I am just working on a project in which i need to raise and handle a custom event... i just simplified a little bit the code and got something like this:

class Car
{
    public int Speed { get; set; }

    public delegate void SpeedTooHigh(string message);

    public event SpeedTooHigh OnSpeedToHigh;

    public Car(int speed)
    {
        this.Speed = speed;

        if (speed >= 100)
        {
            if (this.OnSpeedToHigh != null)
            {
                this.OnSpeedToHigh("Car has a too high speed !");
            }
        }
    }
}

and the main class in which i am using this class:

class Program
{
    static void Main(string[] args)
    {
        Car car = new Car(120, "Red", "Renault");

        car.OnSpeedToHigh += OnCarSpeedToHigh;

        Console.WriteLine("Test events");

        Console.ReadKey();
    }

    static void OnCarSpeedToHigh(string message)
    {
        Console.WriteLine(message);
    }
}

When i am running this example it seems that all the time the "OnSpeedToHigh" is null in Car class. And i do not understand why since i am creating an instance of this class in main class and set the speed to be greater the 100 so that "this.OnSpeedToHigh("Car has a too high speed !")" to be called.

Is this enough for raising the event, to instantiate the class and set the speed to be greater the 100 for example ?

Please let me know about this.

like image 401
Clock Avatar asked May 24 '13 20:05

Clock


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You're firing the event in the constructor, but you're not adding an event handler to the event until after the object is constructed.

Since you haven't yet added any event handlers at the time you fire the event, the event is null.

You probably don't want to fire the event in the constructor, you don't want to be using events at all, or you want the event to be static, so that you can add the event handler before the car is constructed.

like image 138
Servy Avatar answered Sep 22 '22 06:09

Servy