Sunteți pe pagina 1din 7

mystring.Substring(Math.Max(0, mystring.Length - 4)); var result = mystring.Substring(mystring.Length - Math.Min(4, mystring.

Length));

// events1.cs using System; namespace MyCollections { using System.Collections; // A delegate type for hooking up change notifications. public delegate void ChangedEventHandler(object sender, EventArgs e); // A class that works just like ArrayList, but sends event // notifications whenever the list changes. public class ListWithChangedEvent: ArrayList { // An event that clients can use to be notified whenever the // elements of the list change. public event ChangedEventHandler Changed; // Invoke the Changed event; called whenever list changes protected virtual void OnChanged(EventArgs e) { if (Changed != null) Changed(this, e); } // Override some of the methods that can change the list; // invoke event after each public override int Add(object value) { int i = base.Add(value); OnChanged(EventArgs.Empty); return i; } public override void Clear() { base.Clear(); OnChanged(EventArgs.Empty); } public override object this[int index] { set { base[index] = value; OnChanged(EventArgs.Empty); } } } }

namespace TestEvents { using MyCollections; class EventListener { private ListWithChangedEvent List; public EventListener(ListWithChangedEvent list) { List = list; // Add "ListChanged" to the Changed event on "List". List.Changed += new ChangedEventHandler(ListChanged); } // This will be called whenever the list changes. private void ListChanged(object sender, EventArgs e) { Console.WriteLine("This is called when the event fires."); } public void Detach() { // Detach the event and delete the list List.Changed -= new ChangedEventHandler(ListChanged); List = null; } } class Test { // Test the ListWithChangedEvent class. public static void Main() { // Create a new list. ListWithChangedEvent list = new ListWithChangedEvent(); // Create a class that listens to the list's change event. EventListener listener = new EventListener(list); // Add and remove items from the list. list.Add("item 1"); list.Clear(); listener.Detach(); } } } Output This is called when the event fires. This is called when the event fires.

Say I have a class named Frog, it looks like: public class Frog { public int Location { get; set; } public int JumpCount { get; set; } public void OnJump() { JumpCount++; } } I need help with 2 things: I want to create an event named Jump in the class definition. I want to create an instance of the Frog class, and then create another meth od that will be called when the Frog jumps.

public event EventHandler Jump; public void OnJump() { EventHandler handler = Jump; if (null != handler) handler(this, EventArgs.Empty); } then Frog frog = new Frog(); frog.Jump += new EventHandler(yourMethod); private void yourMethod(object s, EventArgs e) { Console.WriteLine("Frog has Jumped!"); }

Let's begin with the CarArgs class: Here's the code: // custom attributes public class CarArgs : System.EventArgs { private string message; public CarArgs(string m) { this.message=m; } public string Message() { return message; } } This class will serve in the declaration of the function we need to call when th e event is raised. It's very simple, a message property, set in the constructor, and a Message() me thod to retrieve it. Note the inheritance from System.EventArgs. Now the Car class; here's the code: // custom object public class Car { private int tank; // delegate declaration public delegate void ChangingHandler (object sender, CarArgs ca); // event declaration public event ChangingHandler Change; public Car(int n) { this.tank=n; } public void SetTank(int p) { this.tank=p; // call the event if (p<5) { CarArgs ca=new CarArgs("Tank is below 5, tank value is : "+this.tank ); Change(this,ca); } } public string GetTank() { return this.tank.ToString(); }

} This is a little bit more difficult to read. There is our tank property, then you can see the declaration of a custom delegat e ChanginHandler. This is what we have to call when the Change event is raised. The event declaration follows the delegate's one. Basically we tell the compiler that we want to apply that delegate to that event. Notice the SetTank() method. If tank goes below 5 litres it raises the event. Fi rst it creates CarArgs and then it calls the event Change() passing a pointer to itself (the object) and the CarArgs just created. Now the testing class: I will report here just the effective code to do the job. You can download the complete source code from my OsiDrive. Here is the link: h ttp://zeus.osix.net:80/modules/folder/index.php?tid=6539&action=vf The idea is this: I have created a buttom on a form and when I press it, a car o bject is being created and tested. Here's the code: private void button1_Click(object sender, System.EventArgs e) { Car car=new Car(50); car.Change+=new Car.ChangingHandler(car_Change); // doesn't raise event car.SetTank(40); // doesn't raise event car.SetTank(20); // raise an event car.SetTank(4); } private void car_Change(object sender, CarArgs ca) { MessageBox.Show(ca.Message()); }

// events2.cs

using System; namespace MyCollections { using System.Collections; // A class that works just like ArrayList, but sends event // notifications whenever the list changes: public class ListWithChangedEvent: ArrayList { // An event that clients can use to be notified whenever the // elements of the list change: public event EventHandler Changed; // Invoke the Changed event; called whenever list changes: protected virtual void OnChanged(EventArgs e) { if (Changed != null) Changed(this,e); } // Override some of the methods that can change the list; // invoke event after each: public override int Add(object value) { int i = base.Add(value); OnChanged(EventArgs.Empty); return i; } public override void Clear() { base.Clear(); OnChanged(EventArgs.Empty); } public override object this[int index] { set { base[index] = value; OnChanged(EventArgs.Empty); } } } } namespace TestEvents { using MyCollections; class EventListener { private ListWithChangedEvent List; public EventListener(ListWithChangedEvent list) { List = list; // Add "ListChanged" to the Changed event on "List": List.Changed += new EventHandler(ListChanged); }

// This will be called whenever the list changes: private void ListChanged(object sender, EventArgs e) { Console.WriteLine("This is called when the event fires."); } public void Detach() { // Detach the event and delete the list: List.Changed -= new EventHandler(ListChanged); List = null; } } class Test { // Test the ListWithChangedEvent class: public static void Main() { // Create a new list: ListWithChangedEvent list = new ListWithChangedEvent(); // Create a class that listens to the list's change event: EventListener listener = new EventListener(list); // Add and remove items from the list: list.Add("item 1"); list.Clear(); listener.Detach(); } } } Output This is called when the event fires. This is called when the event fires.

S-ar putea să vă placă și