|
-
Mar 13th, 2002, 12:43 PM
#1
Event syntax?
what is the syntax to declare and raise events in C#?
-
Mar 13th, 2002, 03:17 PM
#2
Hyperactive Member
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
-
Mar 13th, 2002, 11:21 PM
#3
Hyperactive Member
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 name, int max, int 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", 100, 10);
// 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 = 0; i < 10; i++)
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 = 0; i < 10; i++) c1.SpeedUp(20);
return 0;
}
}
Let me know if you have any questions about the above...
-scott
he he he
-
Mar 14th, 2002, 09:44 AM
#4
looks easy enough..thanks bud.
-
Mar 14th, 2002, 10:22 AM
#5
Hyperactive Member
-scott
he he he
-
Mar 15th, 2002, 01:30 PM
#6
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
-
Mar 15th, 2002, 01:42 PM
#7
Addicted Member
i also have Troelson's VB book (probably gonna get his C# one too), and it IS a direct translation of his C# book.
-
Mar 15th, 2002, 01:44 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|