Results 1 to 8 of 8

Thread: Event syntax?

  1. #1

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Event syntax?

    what is the syntax to declare and raise events in C#?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    Cander,
    Look up delegates. This is the new way of doing events. If you want more than one client to receive event notifications, look up MulticastDelegate.

    They are more similar to call back functions than VB events.

    I will post more pertinent info when I get home. That's where all my .net stuff is.
    -scott
    he he he

  3. #3
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    as promised

    (code from Troelson)

    >> Car class with events
    PHP Code:
    namespace CarEvents
    {
    using System;

    public class 
    Car
    {

        public class 
    Radio
        
    {
            public 
    Radio()
            {}

            public 
    void TurnOn(bool on)
            {
                if(
    on)
                    
    Console.WriteLine("Jamming...");
                else
                    
    Console.WriteLine("Quiet time...");
            }
        }

        
    // The delegate for our events.
        
    public delegate void EngineHandler(string msg);

        
    // This car can send these events.
        
    public static event EngineHandler Exploded;
        public static 
    event EngineHandler AboutToBlow;

        
    // Internal state data.
        
    private int currSpeed;
        private 
    int maxSpeed;
        private 
    string petName;

        
    // Is the car alive or dead?
        
    private bool dead;

        
    // A car has-a radio.
        
    private Radio theMusicBox;

        public 
    Car()
        {
            
    maxSpeed 100;
            
    dead false;
            
    theMusicBox = new Radio();
        }

        public 
    Car(string nameint maxint curr)
        {
            
    currSpeed curr;
            
    maxSpeed max;
            
    petName name;
            
    dead false;
            
    theMusicBox = new Radio();
        }

        public 
    void CrankTunes(bool state)
        {
            
    theMusicBox.TurnOn(state);
        }

        public 
    void SpeedUp(int delta)
        {
            
    // If the car is dead, send event.
            
    if(dead)
            {
                if(
    Exploded != null)
                    
    Exploded("Sorry, this car is dead...");
            }
            else    
            {
                
    currSpeed += delta;
                
                
    // Almost dead?
                
    if(10 == maxSpeed currSpeed)
                    if(
    AboutToBlow != null)
                        
    AboutToBlow("Careful, approaching terminal speed!");

                
    // Still OK!
                
    if(currSpeed >= maxSpeed)
                    
    dead true;
                else
                    
    Console.WriteLine("--> CurrSpeed = " currSpeed);
            }
        }

    }

    >> car console application
    PHP Code:
    namespace CarEvents
    {
    using System;


    // Car event sink
    public class CarEventSink
    {
        
    // OnBlowUp event sink A.
        
    public void OnBlowUp(string s)
        {
                
    Console.WriteLine("Message from car: {0}"s);
        }

        
    // OnBlowUp event sink B.
        
    public void OnBlowUp2(string s)
        {
            
    Console.WriteLine("-->AGAIN I say: {0}"s);
        }

        
    // OnAboutToBlow event sink.
        
    public void OnAboutToBlow(string s)
        {
                
    Console.WriteLine("Message from car: {0}"s);
        }
    }

    // Make a car and listen to the events.
    public class CarApp
    {
        public static 
    int Main(string[] args)
        {
            
    // Make a car as usual. 
            
    Car c1 = new Car("SlugBug"10010);

            
    // Make sink object.
            
    CarEventSink sink = new CarEventSink();

            
    // Hook into events.
            
    Car.Exploded += new Car.EngineHandler(sink.OnBlowUp);
            
    Car.Exploded += new Car.EngineHandler(sink.OnBlowUp2);
            
    Car.AboutToBlow += new Car.EngineHandler(sink.OnAboutToBlow);

            
    // Speed up (this will generate the events.)
            
    for(int i 010i++)
                
    c1.SpeedUp(20);

            
    // Detach from events.
            
    Car.Exploded -= new Car.EngineHandler(sink.OnBlowUp);
            
    Car.Exploded -= new Car.EngineHandler(sink.OnBlowUp2);
            
    Car.Exploded -= new Car.EngineHandler(sink.OnAboutToBlow);

            
    // No response! 
            
    for(int i 010i++) c1.SpeedUp(20);

            return 
    0;
        }

    Let me know if you have any questions about the above...
    -scott
    he he he

  4. #4

    Thread Starter
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    looks easy enough..thanks bud.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327
    no sweat...
    -scott
    he he he

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    wow, I've read Troelson's book about VB.Net and the code looks exactly the same Sounds like he wrote one book and then he translated it to VB.Net

  7. #7
    Addicted Member donut's Avatar
    Join Date
    Mar 2001
    Location
    London, UK
    Posts
    165
    i also have Troelson's VB book (probably gonna get his C# one too), and it IS a direct translation of his C# book.

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    So this way learning C# is easy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width