Delegates
Next, let’s declare a delegate in the class for the OnPrime event (this declaration can go anywhere
within the class module). Note that you don’t really need to create a delegate specially for an event
method if its signature matches the built-in System.EventHandler delegate, but it is good practice to
declare a delegate for each custom event. (We would have to in this example because of the added
parameter in the signature, the prime number found.)
Here’s the delegate declaration:
public delegate void OnPrimeEventHandler (object sender,
System.EventArgs e, long num);
The next thing to do is to associate the delegate with the event:
public event OnPrimeEventHandler OnPrime;
The class module is now complete—although until you create the code in the receiving class it doesn’t
do anything useful (if an event is fired and there are no trees in the forest, does anyone hear it?).
If you run the project, you should see no syntax errors. Here’s the complete code for the class module:
using System;
namespace Builder2
{
public class Class1
{
 public Class1()
 {
 }
 public delegate void OnPrimeEventHandler
  (object sender, System.EventArgs e, long num);
 public event OnPrimeEventHandler OnPrime;
 public void IsPrime (long NumToCheck ) {
  for (long i =2; i <= Math.Sqrt(NumToCheck); i++) {
   if (NumToCheck%i == 0 ) {
    // not a prime
    return;
   }
  }
  // is a prime, fire the event
  System.EventArgs p = new System.EventArgs();
  OnPrime (this, p, NumToCheck);
  return;
 }
}
}
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|