Braintique.com header
Left Navigation Bar

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;
}
}
}

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write